如何告诉我的DataTemplate绑定到PARENT ViewModel中的属性? [英] How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?
问题描述
我有以下 MainView.xaml 文件,可以作为MVVM菜单切换器使用。我有这些对:
I've got the following MainView.xaml file that works well as a MVVM menu switcher. I've got these pairs:
- Page1View / Page1ViewModel
- Page2View / Page2ViewModel 我的 MainViewModel 中的
- Page1View / Page1ViewModel
- Page2View / Page2ViewModel
我用两个ViewModels填充了一个ObservableCollection,当用户点击 Next 按钮,它调用MainViewModel中的 NextPageCommand ,它用一个新的ViewModel切换 CurrentPageViewModel ,然后使用适当的视图显示。
in my MainViewModel I fill an ObservableCollection with both ViewModels, then when the user clicks the Next button, it calls NextPageCommand in MainViewModel which switches out CurrentPageViewModel with a new ViewModel which is then displayed with an appropriate View, works nicely.
我还有一个菜单中填充了来自Observable集合中ViewModels的所有标题,这也很好用。
I also have a Menu being filled with all the titles from the ViewModels in the Observable collection, which also works nicely.
然而,每个MenuItem有一个Command ={Binding SwitchPageCommand},它应该在 MainViewModel 上调用SwitchPageCommand,而不是例如 Page1ViewModel 或 Page2ViewModel 。
However, each MenuItem has a Command="{Binding SwitchPageCommand}" which SHOULD call SwitchPageCommand on the MainViewModel and not on e.g. Page1ViewModel or Page2ViewModel.
那么如何在模板中指出不要绑定到当前ViewModel,但是ViewModel 包含 ViewModel,例如如下所示:
So how can I indicate in the template not to bind to the current ViewModel but the ViewModel which contains that ViewModel, e.g. something like this:
PSEUDO-CODE:
<DataTemplate x:Key="CodeGenerationMenuTemplate">
<MenuItem
Command="{Binding <parentViewModel>.SwitchPageCommand}"
Header="{Binding Title}"
CommandParameter="{Binding Title}"/>
</DataTemplate>
这里是 MainViewModel :
<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">
<Window.Resources>
<DataTemplate x:Key="CodeGenerationMenuTemplate">
<MenuItem Header="{Binding Title}" Command="{Binding SwitchPageCommand}" CommandParameter="{Binding Title}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Page1ViewModel}">
<v:Page1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Page2ViewModel}">
<v:Page2View/>
</DataTemplate>
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="Code _Generation" ItemsSource="{Binding AllPageViewModels}"
ItemTemplate="{StaticResource CodeGenerationMenuTemplate}"/>
</Menu>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Margin="5" Content="Next Page" Command="{Binding NextPageCommand}"/>
</StackPanel>
<ContentControl
Content="{Binding CurrentPageViewModel}"/>
</DockPanel>
</Window>
推荐答案
答案是这样的:
<DataTemplate x:Key="CodeGenerationMenuTemplate">
<MenuItem
Header="{Binding Title}"
Command="{Binding DataContext.SwitchPageCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}"
CommandParameter="{Binding Title}"/>
</DataTemplate>
我刚刚看到,Nir给了我解决上述问题的语法:什么是最好的在MVVM中构建一个显示各种页面的菜单?。
I just saw that Nir had given me the syntax to solve the above issue on this question: What is the best way in MVVM to build a menu that displays various pages?.
这篇关于如何告诉我的DataTemplate绑定到PARENT ViewModel中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!