C# 绑定WPF上下文菜单的标题
我在这里遇到TreeView绑定和ContextMenu问题: 现在我遇到了这个问题:我有上下文菜单C# 绑定WPF上下文菜单的标题,c#,wpf,binding,treeview,contextmenu,C#,Wpf,Binding,Treeview,Contextmenu,我在这里遇到TreeView绑定和ContextMenu问题: 现在我遇到了这个问题:我有上下文菜单 <TreeView.ContextMenu> <ContextMenu x:Name="MyContext" ItemsSource="{Binding OCContext}" DisplayMemberPath="Text"/> </TreeView.ContextMenu> 是否有可能通过绑定获取所选项目?不知道您是否尝试过,但上下文菜单有一个P
<TreeView.ContextMenu>
<ContextMenu x:Name="MyContext" ItemsSource="{Binding OCContext}" DisplayMemberPath="Text"/>
</TreeView.ContextMenu>
是否有可能通过绑定获取所选项目?不知道您是否尝试过,但上下文菜单有一个PlacementTarget,它为您提供了包含上下文菜单的对象 在我的一个项目中,我做了这样的事情:
private void MyContext_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
System.Windows.Controls.Primitives.MenuBase s = sender as System.Windows.Controls.Primitives.MenuBase;
ItemCollection ic = s.Items;
T_Antwort SelectedItem = (T_Antwort)ic.CurrentItem;
}
<MenuItem ... Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ContextMenu}},Path=PlacementTarget.SelectedItem
谢谢,但我不知道应该在哪里使用它。我已经用MenuItem和CommandBinding尝试过了,上面的命令作为CommandParameter,但是命令没有被执行Command=“{Binding asd}”CommandParameter=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ContextMenu}},Path=PlacementTarget.SelectedItem}”
,VMasd=newrelaycommand(asdexecute)代码>。它也是第二个节点,所以MenuItem不太适合。我不知道mvvm…对不起…我是用传统的wpf说的。如果你想要一个例子,我可以给你一个完整的例子在传统的wpf。
<MenuItem ... Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ContextMenu}},Path=PlacementTarget.SelectedItem