Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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 MVVM绑定命令到treeview上下文菜单_C#_Wpf_Xaml_Mvvm_Treeview - Fatal编程技术网

C# WPF MVVM绑定命令到treeview上下文菜单

C# WPF MVVM绑定命令到treeview上下文菜单,c#,wpf,xaml,mvvm,treeview,C#,Wpf,Xaml,Mvvm,Treeview,我有一个树状视图,我想右击菜单。单击菜单项时,应使用该项调用命令。我尝试了不同的解决方案,但不断出现如下错误 System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpressio

我有一个树状视图,我想右击菜单。单击菜单项时,应使用该项调用命令。我尝试了不同的解决方案,但不断出现如下错误

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression
我的树视图如下

   <TreeView ItemsSource="{Binding Buildings}" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Grid.RowSpan="4">
                    <TreeView.Resources>
                        <HierarchicalDataTemplate  ItemsSource="{Binding Gates}" DataType="{x:Type local:Floor}">

                            <TextBlock Text="{Binding Name}" />
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Floors}" DataType="{x:Type local:Building}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}" >
                                    <TextBlock.ContextMenu>
                                        <ContextMenu>
                                            <ContextMenu.ItemContainerStyle>
                                                <Style TargetType="MenuItem">
                                                    <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.EditCmd}"/>
                                                </Style>
                                            </ContextMenu.ItemContainerStyle>
                                        </ContextMenu>
                                    </TextBlock.ContextMenu>
                                </TextBlock>
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <DataTemplate DataType="{x:Type local:Gate}">
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </TreeView.Resources>
                </TreeView>

我尝试了不同的解决方案,例如,但无法解决问题

用户控件
不是
菜单项
的可视祖先,因为
上下文菜单
位于其自己的元素树中

但是您可以将
文本块的
标记
属性绑定到
用户控件
,然后将
菜单项的
命令
属性绑定到
上下文菜单
放置目标
属性:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.EditCmd}"/>
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</StackPanel>


它的可能副本没有treeview,我已经尝试了那里提供的解决方案。谢谢。您尝试过Path=Tag.EditCmd吗?这是我在上面的解决方案中遇到的错误。System.Windows.Data错误:4:找不到引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.UserControl',AncestorLevel='1'的绑定源。BindingExpression:(无路径);DataItem=null;目标元素是“TreeView”(名称=“”);目标属性为“Tag”(类型为“Object”)那么TreeView是否真的位于UserControl内部?我对此表示怀疑。请发布您的完整XAML。
<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.EditCmd}"/>
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</StackPanel>