Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I tell my DataTemplate to bind to a property in the PARENT ViewModel?
    primarykey
    data
    text
    <p>I've got the following <strong>MainView.xaml</strong> file that works well as a MVVM menu switcher. I've got these pairs:</p> <ul> <li>Page1View / Page1ViewModel</li> <li>Page2View / Page2ViewModel</li> </ul> <p>in my <strong>MainViewModel</strong> I fill an ObservableCollection with both ViewModels, then when the user clicks the <strong>Next</strong> button, it calls <strong>NextPageCommand</strong> in MainViewModel which switches out <strong>CurrentPageViewModel</strong> with a new ViewModel which is then displayed with an appropriate View, works nicely.</p> <p>I also have a Menu being filled with all the titles from the ViewModels in the Observable collection, which also works nicely.</p> <p>However, each MenuItem has a Command="{Binding SwitchPageCommand}" which SHOULD call SwitchPageCommand on the <strong>MainViewModel</strong> and not on e.g. <strong>Page1ViewModel</strong> or <strong>Page2ViewModel</strong>.</p> <p><strong>So how can I indicate in the template not to bind to the current ViewModel but the ViewModel which <em>contains</em> that ViewModel, e.g. something like this:</strong></p> <pre><code>PSEUDO-CODE: &lt;DataTemplate x:Key="CodeGenerationMenuTemplate"&gt; &lt;MenuItem Command="{Binding &lt;parentViewModel&gt;.SwitchPageCommand}" Header="{Binding Title}" CommandParameter="{Binding Title}"/&gt; &lt;/DataTemplate&gt; </code></pre> <p>Here is <strong>MainViewModel</strong>:</p> <pre><code>&lt;Window x:Class="TestMenu234.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:TestMenu234.Commands" xmlns:vm="clr-namespace:TestMenu234.ViewModels" xmlns:v="clr-namespace:TestMenu234.Views" Title="Main Window" Height="400" Width="800"&gt; &lt;Window.Resources&gt; &lt;DataTemplate x:Key="CodeGenerationMenuTemplate"&gt; &lt;MenuItem Header="{Binding Title}" Command="{Binding SwitchPageCommand}" CommandParameter="{Binding Title}"/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type vm:Page1ViewModel}"&gt; &lt;v:Page1View/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type vm:Page2ViewModel}"&gt; &lt;v:Page2View/&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;DockPanel&gt; &lt;Menu DockPanel.Dock="Top"&gt; &lt;MenuItem Header="Code _Generation" ItemsSource="{Binding AllPageViewModels}" ItemTemplate="{StaticResource CodeGenerationMenuTemplate}"/&gt; &lt;/Menu&gt; &lt;StackPanel DockPanel.Dock="Top" Orientation="Horizontal"&gt; &lt;Button Margin="5" Content="Next Page" Command="{Binding NextPageCommand}"/&gt; &lt;/StackPanel&gt; &lt;ContentControl Content="{Binding CurrentPageViewModel}"/&gt; &lt;/DockPanel&gt; &lt;/Window&gt; </code></pre>
    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.
 

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