Note that there are some explanatory texts on larger screens.

plurals
  1. POExecute same Prism Command from different ViewModels
    text
    copied!<p>Is it possible to execute somehow one Command from a different ViewModels in the WPF application using Prism?</p> <p>Let me explain what I mean.</p> <p>I have the MainMenuViewModel class:</p> <pre><code>public class MainMenuViewModel { private ICommand _aboutCommand; public ICommand AboutCommand { get { if (_aboutCommand == null) { _aboutCommand = new DelegateCommand(() =&gt; { MessageBox.Show("About menu item clicked!"); }); } return _aboutCommand; } } } </code></pre> <p>also there is the View for this model:</p> <pre><code>&lt;Menu IsMainMenu="True"&gt; &lt;MenuItem Header="Nápověda"&gt; &lt;MenuItem Header="O Aplikaci" x:Name="About" Command="{Binding AboutCommand}" /&gt; &lt;/MenuItem&gt; &lt;/Menu&gt; </code></pre> <p>There is another module in the application, which should execute the command with the same behavior (or even probably better - the same command):</p> <pre><code>public class MunisatorViewModel { private ICommand _aboutCommandInAnotherModule; public ICommand AboutCommandInAnotherModule { get { if (_aboutCommandInAnotherModule== null) { _aboutCommandInAnotherModule= new DelegateCommand(() =&gt; { MessageBox.Show("About menu item clicked!"); }); } return _aboutCommandInAnotherModule; } } } </code></pre> <p>this module has the View:</p> <pre><code>&lt;StackPanel Background="White" HorizontalAlignment="Center" VerticalAlignment="Top"&gt; &lt;Button cmd:Click.Command="{Binding AboutCommandInAnotherModule}"&gt;About&lt;/Button&gt; &lt;/StackPanel&gt; </code></pre> <p>Is it possible to avoid the duplicate code?</p> <p>P.S. I understand, that I can create base class for this two ViewModels and describe this Commands there, but problem is, that some of the ViewModels has already different base classes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload