如何从 UserControl 的上下文菜单中引用 UserControl 样式中的元素?
How to reference an element in the UserControl's style from the UserControl's ContextMenu?
给定包含上下文菜单和控件模板的 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>
上下文菜单是一个弹出窗口,它在自己的可视化树中呈现,而不是作为其父项的子项。为了实现你需要做的事情,你可以做这样的事情
<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>
给定包含上下文菜单和控件模板的 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>
上下文菜单是一个弹出窗口,它在自己的可视化树中呈现,而不是作为其父项的子项。为了实现你需要做的事情,你可以做这样的事情
<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>