【问题标题】:How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?如何告诉我的 DataTemplate 绑定到 PARENT ViewModel 中的属性?
【发布时间】:2010-11-04 19:25:52
【问题描述】:

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

  • Page1View / Page1ViewModel
  • Page2View / Page2ViewModel

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

我还有一个菜单,其中填充了 Observable 集合中 ViewModel 的所有标题,这也很好用。

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

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

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>

【问题讨论】:

    标签: wpf data-binding xaml mvvm


    【解决方案1】:

    答案是这样的:

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

    我刚刚看到 Nir ​​给了我解决上述问题的语法:What is the best way in MVVM to build a menu that displays various pages?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2016-01-11
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多