简体   繁体   English

WPF上下文菜单不会绑定到VIewModel属性

[英]WPF Context Menu won't Bind To VIewModel property

I am having a WPF binding issue that I cannot figure out. 我遇到无法解决的WPF绑定问题。 I have a ContextMenu template that is formatted as shown: 我有一个ContextMenu模板,其格式如下所示:

<ContextMenu x:Key="CopyPasteContextMenu">
    <MenuItem Header="AlternateDelete"
              Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand, 
              RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
</ContextMenu>

The context menu is being used in the DataTemplat, and the binding for the Tag on the Border is finding the PropertyEditorView correctly, I just can't get it from the border to the contextmenu. 在DataTemplat中使用了上下文菜单,并且边框上的Tag的绑定正在正确找到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>

The tag can bind properly to my view model which is called “PropertyEditorViewModel”. 标签可以正确地绑定到名为“ PropertyEditorViewModel”的视图模型。 I can see this while debugging the system in the visual tree. 我可以在可视化树中调试系统时看到这一点。 When I drill into my Context Menu, the binding is not happening properly. 当我进入“上下文菜单”时,绑定未正确进行。

For my Command to work, I need it to bind properly to the Command to PropertyEditorView view model command called “AlternateDeleteCommand”. 为了使我的命令正常工作,我需要将其正确绑定到名为“ AlternateDeleteCommand”的Command to PropertyEditorView视图模型命令。

public class PropertyEditorViewModel : DisposableViewModelBase, IPropertyEditorViewModel
{
    public ICommand AlternateDeleteCommand { get; set; }

Looked at this for a day so far, and not sure why my binding isn't working on the context menu, anyone got something I'm missing? 到目前为止,已经看了一天,不确定为什么我的绑定在上下文菜单上不起作用,有人得到了我所缺少的东西吗?

Thanks! 谢谢!

Doesn't the relative source need to be on the context menu and not on the menu item? 相对源是否不需要位于上下文菜单上,而不必位于菜单项上? Since you are checking the placement target of the context menu? 由于您正在检查上下文菜单的放置目标?

<MenuItem Header="AlternateDelete"
          Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand, 
          RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Mode=OneWay}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM