Wpf 使用MVVM(ItemsSource)尝试查找DataContext的ContextMenu

Wpf 使用MVVM(ItemsSource)尝试查找DataContext的ContextMenu,wpf,binding,contextmenu,Wpf,Binding,Contextmenu,我正在努力使这个绑定在我的上下文菜单上工作 我正在使用Items Source,因此每个项都属于frm ItemsSource类型,因此我需要搜索树以获得正确的绑定上下文 我尝试执行的命令存在于ContextMenu的DataContext中,您可以在此处看到它: DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" 因此,我尝试使用RelativeSource绑定到该数据上下文,但无论我尝试

我正在努力使这个绑定在我的上下文菜单上工作

我正在使用Items Source,因此每个项都属于frm ItemsSource类型,因此我需要搜索树以获得正确的绑定上下文

我尝试执行的命令存在于ContextMenu的DataContext中,您可以在此处看到它:

DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}"
因此,我尝试使用RelativeSource绑定到该数据上下文,但无论我尝试了什么,我总是会得到一个绑定错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'CreateNodeCommand' property not found on 'object' ''NodeGraphView' (Name='networkControl')'. BindingExpression:Path=DataContext.CreateNodeCommand; DataItem='ContextMenu' (Name='NodeGraphRightClickMenu'); target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')
代码如下:

  <ContextMenu x:Name="NodeGraphRightClickMenu" DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" ItemsSource="{Binding DataContext.PolymorphicTypes}">
<ContextMenu.ItemContainerStyle>
    <Style TargetType="MenuItem">
        <Setter Property="Command" Value="{Binding DataContext.CreateNodeCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
        <Setter Property="CommandParameter">
            <Setter.Value>
                <MultiBinding Converter="{convertersDF:Converter_MultiBindingToArray}">
                    <Binding Path="."></Binding>
                    <Binding Path="Name"></Binding>
                </MultiBinding>
            </Setter.Value>
        </Setter>
        <Setter Property="Header">
            <Setter.Value>
                <Binding StringFormat="Create {0}" Path="Name"></Binding>
            </Setter.Value>
        </Setter>
    </Style>
</ContextMenu.ItemContainerStyle>


非常感谢您的帮助。

答案很明显。我需要绑定到DataContext的DataContext。这让人困惑,所以我重新写了一遍:

                                <ContextMenu x:Name="NodeGraphRightClickMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" ItemsSource="{Binding PolymorphicTypes}">
                                    <ContextMenu.ItemContainerStyle>
                                        <Style TargetType="MenuItem">
                                            <Setter Property="Command" Value="{Binding DataContext.CreateNodeCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                                            <Setter Property="CommandParameter">
                                                <Setter.Value>
                                                    <MultiBinding Converter="{convertersDF:Converter_MultiBindingToArray}">
                                                        <Binding Path="."></Binding>
                                                        <Binding Path="Name"></Binding>
                                                    </MultiBinding>
                                                </Setter.Value>
                                            </Setter>
                                            <Setter Property="Header">
                                                <Setter.Value>
                                                    <Binding StringFormat="Create {0}" Path="Name"></Binding>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </ContextMenu.ItemContainerStyle>
                                </ContextMenu>

您是否检查了酒店名称?检查是否有拼写错误