Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定WPF上下文菜单的标题_C#_Wpf_Binding_Treeview_Contextmenu - Fatal编程技术网

C# 绑定WPF上下文菜单的标题

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问题:

现在我遇到了这个问题:我有上下文菜单

<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}”
,VM
asd=newrelaycommand(asdexecute)。它也是第二个节点,所以MenuItem不太适合。我不知道mvvm…对不起…我是用传统的wpf说的。如果你想要一个例子,我可以给你一个完整的例子在传统的wpf。
 <MenuItem ...    Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ContextMenu}},Path=PlacementTarget.SelectedItem