Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
WPF usercontrol命令绑定到窗口视图模型_Wpf_Binding_Mvvm - Fatal编程技术网

WPF usercontrol命令绑定到窗口视图模型

WPF usercontrol命令绑定到窗口视图模型,wpf,binding,mvvm,Wpf,Binding,Mvvm,我有一个包含UserControl1和UserControl2的窗口。这些用户控件有自己的ViewModel。此外,这些用户控件使用UserControl3显示数据。因此,当UserControl1使用UserControl3时,UserControl3与UserControl1具有相同的视图模型 我在UserControl3中有一个绑定,我希望调用UserControl1的viewmodel上的命令 但我找不到办法让它工作。欢迎任何帮助。多谢各位 这是我的装订不起作用: <UserCon

我有一个包含UserControl1和UserControl2的窗口。这些用户控件有自己的ViewModel。此外,这些用户控件使用UserControl3显示数据。因此,当UserControl1使用UserControl3时,UserControl3与UserControl1具有相同的视图模型

我在UserControl3中有一个绑定,我希望调用UserControl1的viewmodel上的命令

但我找不到办法让它工作。欢迎任何帮助。多谢各位

这是我的装订不起作用:

<UserControl x:Class="MyNamespace.UserControl3"             
         xmlns:local="clr-namespace:MyNamespace">    
<UserControl.Resources>        
    <DataTemplate DataType="{x:Type g:GraphNode}"> 
        <StackPanel>               
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="My Command" Command="{Binding Path=DataContext.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:UserControl3}}}"/>
                </ContextMenu>
            </StackPanel.ContextMenu>               
            <Grid>
                <ContentControl Content="{Binding Data}"/>                      
            </Grid> 
        </StackPanel>             
    </DataTemplate>       
</UserControl.Resources>

这在我的应用程序中有效:

<DataTemplate DataType="{x:Type g:GraphNode}">
        <StackPanel Tag="{Binding}">
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="My Command" Command="{Binding Path=PlacementTarget.Tag.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
                </ContextMenu>
            </StackPanel.ContextMenu>
        </StackPanel>
    </DataTemplate>

关键是ContextMenus位于不同的窗口,因此您无法像通常那样访问DataContext。
您必须对此进行调整,以便将包含您搜索的
命令的对象设置为
堆栈面板
(这是您的
上下文菜单
放置目标
)的标记。

这在我的应用程序中起作用:

<DataTemplate DataType="{x:Type g:GraphNode}">
        <StackPanel Tag="{Binding}">
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="My Command" Command="{Binding Path=PlacementTarget.Tag.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
                </ContextMenu>
            </StackPanel.ContextMenu>
        </StackPanel>
    </DataTemplate>

关键是ContextMenus位于不同的窗口,因此您无法像通常那样访问DataContext。
您必须对此进行调整,以便将包含您搜索的
命令的对象设置为
堆栈面板的标记(这是
上下文菜单的
放置目标