WPF-带有子项的菜单项未触发绑定命令 [英] WPF - MenuItem with children not firing bound command
问题描述
所以我整天都把头靠在墙上.
So I've been butting my head against the wall with this one all day.
在我的WPF应用程序(使用MVVM Light)中,我有一个上下文菜单,该菜单绑定到一组视图模型,并且行为不正确.我可以创建菜单,并且一切与我的MenuItems树状运行,正在执行的命令以及正确的参数一起正常运行.我正在使用它制作一个上下文菜单,允许用户向文件夹添加项目.
In my WPF application (using MVVM Light), I have a context menu which is bound to a collection of viewmodels, and it is not behaving quite correctly. I can create my menu, and everything works perfectly with my tree of MenuItems behaving, commands being executed, and the correct parameter coming through. I'm using this to make a context menu which allows a user to add items to folders.
我遇到的问题是,当上下文菜单项包含子项时,将不再触发该命令.因此,我只能将项目添加到没有子文件夹的文件夹中.
The problem that I've run into is that when a context menu item has children, the command is no longer fired. Thusly, I can only add items to folders which have no child folders.
我已经使用Snoop进行了调查,并且我的DataContext对于MenuItem正确显示,并且命令已正确绑定,并且mousedown事件确实被触发.
I've investigated this using Snoop, and my DataContext is showing up correctly for the MenuItem, and the command is bound correctly, and the mousedown event does get fired.
我遇到的问题是,如果MenuItem有子项,则该命令不会被执行.任何没有子项的项目,其命令都没有问题.
我在这里真的很茫然,关于Stack Overflow或MSDN social的每个类似问题仍然没有答案.
I'm really at a loss here, and every similar question on Stack Overflow or MSDN social remains unanswered.
我已将样式设置为绑定.
I've set up my bindings in a style.
<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来自本文,并且按照描述进行工作,以允许我将我用过的控件的数据上下文用作命令参数
Note that the DataSpy comes from this article, and works as described to allow me to use the datacontext of my usedcontrol as a command parameter
http://www.codeproject.com/Articles/27432/WPF中的Artificial-Inheritance-Contexts-in-WPF
这是上下文菜单中使用的视图模型
Here is the viewmodel used for the context menu
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
}
这是上下文项的实际使用方式.
Here's the way a context item is actually used.
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
}
推荐答案
我看到问题出在XAML绑定上.您需要的是 HierarchicalDataTemplate
绑定.该代码不会为我认为导致问题的孩子绑定该命令.
I see the problem is with XAML binding. What you need is HierarchicalDataTemplate
binding. The code doesn't bind the command for the children which I believe is causing the issue.
检查是否有帮助-命令绑定无效在动态MVVM上下文菜单中
这篇关于WPF-带有子项的菜单项未触发绑定命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!