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

后端 未结 1 565
难免孤独
难免孤独 2020-12-05 00:16

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

  • Page1View / Page1ViewModel
相关标签:
1条回答
  • 2020-12-05 00:54

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

    0 讨论(0)
提交回复
热议问题