wpf 如何告诉我的 DataTemplate 绑定到 PARENT ViewModel 中的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1026342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:36:07  来源:igfitidea点击:

How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?

wpfdata-bindingxamlmvvm

提问by Edward Tanguay

I've got the following MainView.xamlfile that works well as a MVVM menu switcher. I've got these pairs:

我有以下MainView.xaml文件,可以很好地用作 MVVM 菜单切换器。我有这些对:

  • Page1View / Page1ViewModel
  • Page2View / Page2ViewModel
  • Page1View / Page1ViewModel
  • Page2View / Page2ViewModel

in my MainViewModelI fill an ObservableCollection with both ViewModels, then when the user clicks the Nextbutton, it calls NextPageCommandin MainViewModel which switches out CurrentPageViewModelwith a new ViewModel which is then displayed with an appropriate View, works nicely.

在我的MainViewModel 中,我用两个 ViewModel 填充 ObservableCollection,然后当用户单击Next按钮时,它调用MainViewModel 中的NextPageCommand,它用新的 ViewModel切换CurrentPageViewModel,然后用适当的视图显示,效果很好。

I also have a Menu being filled with all the titles from the ViewModels in the Observable collection, which also works nicely.

我还有一个 Menu,里面装满了 Observable 集合中 ViewModels 的所有标题,这也很好用。

However, each MenuItem has a Command="{Binding SwitchPageCommand}" which SHOULD call SwitchPageCommand on the MainViewModeland not on e.g. Page1ViewModelor Page2ViewModel.

但是,每个 MenuItem 都有一个 Command="{Binding SwitchPageCommand}" ,它应该在MainViewModel而不是在例如Page1ViewModelPage2ViewModel上调用 SwitchPageCommand 。

So how can I indicate in the template not to bind to the current ViewModel but the ViewModel which containsthat ViewModel, e.g. something like this:

那么如何在模板中指示不绑定到当前的 ViewModel 而是绑定到包含该 ViewModel 的 ViewModel,例如这样的东西:

PSEUDO-CODE:

<DataTemplate x:Key="CodeGenerationMenuTemplate">
    <MenuItem 
        Command="{Binding <parentViewModel>.SwitchPageCommand}" 
        Header="{Binding Title}" 
        CommandParameter="{Binding Title}"/>
</DataTemplate>

Here is MainViewModel:

这是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>

回答by Edward Tanguay

The answer is this:

答案是这样的:

<DataTemplate x:Key="CodeGenerationMenuTemplate">
    <MenuItem 
        Header="{Binding Title}" 
        Command="{Binding DataContext.SwitchPageCommand,
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}" 
        CommandParameter="{Binding Title}"/>
</DataTemplate>

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?.

我刚刚看到 Nir ​​为我提供了解决上述问题的语法:在 MVVM 中构建显示各种页面的菜单的最佳方法是什么?.