.net 使用依赖项属性绑定上下文菜单项
我有一个自定义画布(DesignerCanvas),用户可以向该画布添加一些控件。在添加任何控件之前,画布首先将其包装到自定义控件(设计器项)中,然后将该控件添加到画布中 这与本文中的代码项目一样- WPF图设计器- 我为DesignerCanvas和DesignerItem添加了上下文菜单,其中包含一个名为“Lock”的菜单项。我还在画布中添加了一个名为“IsLocked”的属性来锁定画布 现在,在上下文菜单中,我想在画布是否被锁定时显示一个复选标记,为此,我将IsLocked属性绑定到MenuItem的“IsChecked”属性。问题是它适用于DesignerCanvas cantext菜单,但不适用于DesignerItems上下文菜单 DesignerCanvas ContextMenu代码,这很好用-.net 使用依赖项属性绑定上下文菜单项,.net,wpf,data-binding,canvas,contextmenu,.net,Wpf,Data Binding,Canvas,Contextmenu,我有一个自定义画布(DesignerCanvas),用户可以向该画布添加一些控件。在添加任何控件之前,画布首先将其包装到自定义控件(设计器项)中,然后将该控件添加到画布中 这与本文中的代码项目一样- WPF图设计器- 我为DesignerCanvas和DesignerItem添加了上下文菜单,其中包含一个名为“Lock”的菜单项。我还在画布中添加了一个名为“IsLocked”的属性来锁定画布 现在,在上下文菜单中,我想在画布是否被锁定时显示一个复选标记,为此,我将IsLocked属性绑定到Me
<!-- Context menu for DesignerCanvas -->
<ContextMenu x:Key="DesignerCanvasContextMenu">
<!-- Other menu items -->
<MenuItem IsCheckable="True" Header="Lock"
IsChecked="{Binding Path=IsLocked, Mode=TwoWay,
RelativeSource={RelativeSource AncestorType={x:Type locl:DesignerCanvas}}}"
Command="{x:Static local:DesignerCanvas.LockUnLock}"
CommandParameter="{Binding RelativeSource={RelativeSource Self},
Path=IsChecked}"
CommandTarget="{Binding Path=PlacementTarget,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
</MenuItem>
</ContextMenu>
也需要对DesignerItem菜单执行相同的操作。我试过这么做,但没用-
<!-- Context menu for DesignerItem -->
<ContextMenu x:Key="DesignerItemContextMenu">
<!-- Other menu items -->
<MenuItem IsCheckable="True" Header="Lock"
IsChecked="{Binding Path=Parent.IsLocked, Mode=TwoWay,
RelativeSource={RelativeSource AncestorType={x:Type locl:DesignerItem}}}"
Command="{x:Static local:DesignerCanvas.LockUnLock}"
CommandParameter="{Binding RelativeSource={RelativeSource Self},
Path=IsChecked}"
CommandTarget="{Binding Path=PlacementTarget,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
</MenuItem>
</ContextMenu>
我也尝试过使用PlacementTarget来实现这一点,但没有成功。由于DesignerItem是DesignerCanvas的孩子,因此不应该无法获得DesignerCanvas及其财产。我做错了什么,知道吗
更新:
我附加了DesignerCanvas和Designer项的上下文菜单,如下所示-
<!-- DesignerItem || Style -->
<Style TargetType="{x:Type locl:DesignerItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DesignerItem}">
<Grid x:Name="PART_Grid" Cursor="SizeAll"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}
,Path=.}"
ContextMenu="{StaticResource DesignerItemContextMenu}">
<!-- other template parts -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- DesignerCanvas || Style -->
<Style
TargetType="{x:Type locl:DesignerCanvas}">
<Setter
Property="ContextMenu"
Value="{StaticResource DesignerCanvasContextMenu}" />
</Style>
快速猜测一下:
您是否尝试过将DesignerItem上的Contextmenu设置为与画布相同的方式?像这样:
<Style TargetType="{x:Type locl:DesignerItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DesignerItem}">
<Grid x:Name="PART_Grid" Cursor="SizeAll"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}
,Path=.}">
<!-- other template parts -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter
Property="ContextMenu"
Value="{StaticResource DesignerItemContextMenu}" />
</Style>
除此之外,我真的建议您启动应用程序并在上下文菜单中查找绑定错误您能显示更多代码吗?如何设置上下文菜单?在输出窗口中是否有任何绑定错误?@redoced-我添加了更多详细信息。我仍然需要调试绑定,如果我看到任何绑定,我会告诉你。谢谢重做,这就是我缺少的。我错误地附加了上下文菜单。更正它并像这样使用它-IsChecked=“{Binding Path=PlacementTarget.Parent.IsLocked,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}”