WPF 上下文菜单不会绑定到 VIewModel 属性
WPF Context Menu won't Bind To VIewModel property
我遇到了无法解决的 WPF 绑定问题。我有一个格式如下所示的 ContextMenu 模板:
<ContextMenu x:Key="CopyPasteContextMenu">
<MenuItem Header="AlternateDelete"
Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand,
RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
</ContextMenu>
上下文菜单正在 DataTemplat 中使用,边框上标签的绑定正确地找到了 PropertyEditorView,我只是无法从边框到上下文菜单。
<DataTemplate x:Key="PropertyValueCellViewingTemplate" DataType="viewModels:IConfigurationItemViewModel">
<Border x:Name="ValueCellBorder"
Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:PropertyEditorView}}}"
ContextMenu="{StaticResource CopyPasteContextMenu}"
Style="{StaticResource PropertyGridValueCellBorderStyle}">
(...)
</Border>
</DataTemplate>
标签可以正确绑定到我的名为“PropertyEditorViewModel”的视图模型。我可以在可视化树中调试系统时看到这一点。当我深入我的上下文菜单时,绑定没有正常进行。
为了让我的命令正常工作,我需要将它正确绑定到名为“AlternateDeleteCommand”的 PropertyEditorView 视图模型命令的命令。
public class PropertyEditorViewModel : DisposableViewModelBase, IPropertyEditorViewModel
{
public ICommand AlternateDeleteCommand { get; set; }
到目前为止已经看了一天,不确定为什么我的绑定在上下文菜单上不起作用,有人知道我遗漏了什么吗?
谢谢!
相对源不需要在上下文菜单上而不是在菜单项上吗?由于您正在检查上下文菜单的放置目标?
<MenuItem Header="AlternateDelete"
Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Mode=OneWay}" />
我遇到了无法解决的 WPF 绑定问题。我有一个格式如下所示的 ContextMenu 模板:
<ContextMenu x:Key="CopyPasteContextMenu">
<MenuItem Header="AlternateDelete"
Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand,
RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
</ContextMenu>
上下文菜单正在 DataTemplat 中使用,边框上标签的绑定正确地找到了 PropertyEditorView,我只是无法从边框到上下文菜单。
<DataTemplate x:Key="PropertyValueCellViewingTemplate" DataType="viewModels:IConfigurationItemViewModel">
<Border x:Name="ValueCellBorder"
Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:PropertyEditorView}}}"
ContextMenu="{StaticResource CopyPasteContextMenu}"
Style="{StaticResource PropertyGridValueCellBorderStyle}">
(...)
</Border>
</DataTemplate>
标签可以正确绑定到我的名为“PropertyEditorViewModel”的视图模型。我可以在可视化树中调试系统时看到这一点。当我深入我的上下文菜单时,绑定没有正常进行。
为了让我的命令正常工作,我需要将它正确绑定到名为“AlternateDeleteCommand”的 PropertyEditorView 视图模型命令的命令。
public class PropertyEditorViewModel : DisposableViewModelBase, IPropertyEditorViewModel
{
public ICommand AlternateDeleteCommand { get; set; }
到目前为止已经看了一天,不确定为什么我的绑定在上下文菜单上不起作用,有人知道我遗漏了什么吗?
谢谢!
相对源不需要在上下文菜单上而不是在菜单项上吗?由于您正在检查上下文菜单的放置目标?
<MenuItem Header="AlternateDelete"
Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Mode=OneWay}" />