WPF - 具有子节点未触发绑定命令的MenuItem

时间:2013-10-24 04:49:53

标签: c# wpf contextmenu mvvm-light menuitem

所以我一整天都把这头撞在了墙上。

在我的WPF应用程序中(使用MVVM Light),我有一个上下文菜单,它绑定到一组视图模型,并且行为不正确。我可以创建我的菜单,一切都与我的MenuItems树的行为完美,正在执行的命令以及正确的参数通过。我正在使用它来创建一个上下文菜单,允许用户将项目添加到文件夹。

我遇到的问题是当上下文菜单项有子项时,不再触发该命令。因此,我只能将项目添加到没有子文件夹的文件夹中。

我使用Snoop对此进行了调查,我的DataContext正在为MenuItem正确显示,并且命令绑定正确,并且mousedown事件会被触发。

我遇到的问题是,如果一个MenuItem有子节点,它的命令就不会被执行。任何没有子项的项目,Command都会毫无问题地执行。

我真的很茫然,Stack Overflow或MSDN社交网上的每个类似问题都没有答案。

我已经设置了一个样式的绑定。

<utility:DataContextSpy x:Key="Spy" />

<!-- Context style (in UserControl.Resources) -->

<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Header" Value="{Binding Header}"/>
    <Setter Property="ItemsSource" Value="{Binding Children}"/>
    <Setter Property="Command" Value="{Binding Command}" />
    <Setter Property="CommandParameter" Value="{Binding DataContext, Source={StaticResource Spy}" />
    <Setter Property="CommandTarget" Value="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</Style>

<!-- Later in the control -->
<ContextMenu ItemContainerStyle="{StaticResource ContextMenuItemStyle}" ItemsSource="{Binding MenuItems}" />

请注意,DataSpy来自本文,并按照描述工作,允许我使用usedcontrol的datacontext作为命令参数

http://www.codeproject.com/Articles/27432/Artificial-Inheritance-Contexts-in-WPF

这是用于上下文菜单的视图模型

public interface IContextMenuItem
{
    string Header { get; }
    IEnumerable<IContextMenuItem> Children { get; }
    ICommand Command { get; set; }
}

public class BasicContextMenuItem : ViewModelBase, IContextMenuItem
{
    #region Declarations

    private string _header;
    private IEnumerable<IContextMenuItem> _children;

    #endregion

    #region Constructor

    public BasicContextMenuItem(string header)
    {
        Header = header;
        Children = new List<IContextMenuItem>();
    }

    #endregion

    #region Observables

    public string Header
    {
        get { return _header; }
        set
        {
            _header = value;
            RaisePropertyChanged("Header");
        }
    }

    public IEnumerable<IContextMenuItem> Children
    {
        get { return _children; }
        set
        {
            _children = value;
            RaisePropertyChanged("Children");
        }
    }

    public ICommand Command { get; set; }

    #endregion
}

以下是实际使用上下文项的方式。

MenuItems = new List<IContextMenuItem>
{
    new BasicContextMenuItem("New Folder") { Command = NewFolderCommand} ,
    new BasicContextMenuItem("Delete Folder") { Command = DeleteFolderCommand },
    new BasicContextMenuItem("Rename") { Command = RenameFolderCommand },
};

public ICommand NewFolderCommand
{
    get { return new RelayCommand<FolderViewModel>(NewFolder); }
}

private void NewFolder(FolderViewModel viewModel)
{
    // Do work
}

1 个答案:

答案 0 :(得分:0)

我发现问题在于XAML绑定。你需要的是HierarchicalDataTemplate绑定。代码没有绑定我认为导致问题的孩子的命令。

检查这是否有帮助 - Command binding not working in a dynamic MVVM Context Menu