如何从UserControl的ContextMenu中引用UserControl风格的元素?

时间:2015-04-12 02:02:55

标签: c# wpf xaml mvvm data-binding

如果用户控件合并了包含上下文菜单和控件模板的MVVM模式,那么如何从上下文菜单中引用样式中的元素?除了MenuItem CommandParameter之外,以下代码似乎工作正常。 “Template.MyViewBox.Child”的绑定路径显然不起作用,但展示了我想要完成的目标。

<UserControl x:Class="MyViewer.Views.IconView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:MyViewer.ViewModels">

<UserControl.Resources>
    <vm:ViewModelLocator x:Key="Locator" />
</UserControl.Resources>

<UserControl.DataContext>
    <Binding Source="{StaticResource Locator}" Path="Main" />
</UserControl.DataContext>

<UserControl.ContextMenu>
    <ContextMenu x:Name="CtContextMenu"
                  DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
        <MenuItem Header="Print Current View"
                  Command="{Binding Path=DataContext.PrintCurrentViewCmd}"
                  CommandParameter="{Binding Path=Template.MyViewBox.Child}"/>
    </ContextMenu>
</UserControl.ContextMenu>

<UserControl.Style>
    <Style TargetType="{x:Type UserControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type UserControl}" >
                    <Viewbox x:Name="MyViewBox">
                        <ContentControl Name="MyContentControl" Content="{TemplateBinding Content}" />
                    </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Style>

1 个答案:

答案 0 :(得分:0)

上下文菜单是一个弹出窗口,它在自己的可视树中呈现,而不是作为其父项的子项。为了实现你需要做的事,你可以做这样的事情

<UserControl x:Class="MyViewer.Views.IconView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:MyViewer.ViewModels" 
         Tag="{Binding Path=Child, RelativeSource={RelativeSource AncestorType={x:Type ViewBox}}}">

<UserControl.Resources>
    <vm:ViewModelLocator x:Key="Locator" />
</UserControl.Resources>

<UserControl.DataContext>
    <Binding Source="{StaticResource Locator}" Path="Main" />
</UserControl.DataContext>

<UserControl.ContextMenu>
    <ContextMenu x:Name="CtContextMenu"
                  DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
        <MenuItem Header="Print Current View"
                  Command="{Binding Path=DataContext.PrintCurrentViewCmd}"
                  CommandParameter="{Binding Path=PlacementTarget.Tag,Element=CtContextMenu}"/>
    </ContextMenu>
</UserControl.ContextMenu>

<UserControl.Style>
    <Style TargetType="{x:Type UserControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type UserControl}" >
                    <Viewbox x:Name="MyViewBox">
                        <ContentControl Name="MyContentControl" Content="{TemplateBinding Content}" />
                    </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Style>