1

所以我整天都用这个把头撞在墙上。

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

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

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

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

我真的很茫然,Stack Overflow 或 MSDN social 上的所有类似问题都没有得到解答。

我已经以一种风格设置了我的绑定。

<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
}
4

1 回答 1

0

我看到问题出在 XAML 绑定上。你需要的是HierarchicalDataTemplate绑定。该代码没有为我认为导致问题的孩子绑定命令。

检查这是否有帮助 -命令绑定在动态 MVVM 上下文菜单中不起作用

于 2013-10-24T06:10:01.470 回答