Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, you don't need any of those toolkits/frameworks to implement MVVM. It can be as simple as this... let's assume that we have a <code>MainViewModel</code>, and <code>PersonViewModel</code> and a <code>CompanyViewModel</code>, each with their own related view and each extending an <code>abstract</code> base class <code>BaseViewModel</code>. </p> <p>In <code>BaseViewModel</code>, we can add common properties and/or <code>ICommand</code> instances and implement the <code>INotifyPropertyChanged</code> interface. As they all extend the <code>BaseViewModel</code> class, we can have this property in the <code>MainViewModel</code> class that can be set to any of our view models:</p> <pre><code>public BaseViewModel ViewModel { get; set; } </code></pre> <p>Of course, you'd be implementing the <code>INotifyPropertyChanged</code> interface correctly on <em>your</em> properties unlike this quick example. Now in <code>App.xaml</code>, we declare some simple <code>DataTemplate</code>s to connect the views with the view models:</p> <pre><code>&lt;DataTemplate DataType="{x:Type ViewModels:MainViewModel}"&gt; &lt;Views:MainView /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type ViewModels:PersonViewModel}"&gt; &lt;Views:PersonView /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type ViewModels:CompanyViewModel}"&gt; &lt;Views:CompanyView /&gt; &lt;/DataTemplate&gt; </code></pre> <p>Now, wherever we use one of our <code>BaseViewModel</code> instances in our application, these <code>DataTemplate</code>s will tell the framework to display the related view instead. We can display them like this:</p> <pre><code>&lt;ContentControl Content="{Binding ViewModel}" /&gt; </code></pre> <p>So all we need to do now to switch to a new view is to set the <code>ViewModel</code> property from the <code>MainViewModel</code> class:</p> <pre><code>ViewModel = new PersonViewModel(); </code></pre> <p>Finally, how do we change the views from other views? Well there are several possible ways to do this, but the easiest way is to add a <code>Binding</code> from the child view directly to an <code>ICommand</code> in the <code>MainViewModel</code>. I use a custom version of the <code>RelayComand</code>, but you can use any type you like and I'm guessing that you'll get the picture:</p> <pre><code>public ICommand DisplayPersonView { get { return new ActionCommand(action =&gt; ViewModel = new PersonViewModel(), canExecute =&gt; !IsViewModelOfType&lt;Person&gt;()); } } </code></pre> <p>In the child view XAML:</p> <pre><code>&lt;Button Command="{Binding DataContext.DisplayPersonView, RelativeSource= {RelativeSource AncestorType={x:Type MainView}}, Mode=OneWay}" /&gt; </code></pre> <p>That's it! Enjoy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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