在上下文菜单中不调用DelegateCommand

我似乎无法让这个右键单击弹出菜单工作。

            
 //Delegated command public DelegateCommand CommandPopupClick { get; set; } //Assigning the delegate command. CommandPopupClick = new DelegateCommand(PopupClick, CanMyCommand); //Event for rightclick option clicked public void PopupClick(Object Parameters) { var something = Parameters; //Break is here to see if the event fires } 

我可以看到弹出菜单中包含“删除”和“属性”项。 但是,当我单击其中任何一个时,它不会触发事件。

注意:委托命令系统适用于其他所有事情,我不认为这是问题所在。

如果我能帮忙的话,我真的不想使用Name.Click += new RoutedEvent()

谢谢。

调试中的错误按请求

 A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll 'Enterprise.exe' (CLR v4.0.30319: Enterprise.exe): Loaded 'Microsoft.GeneratedCode'. 'Enterprise.exe' (CLR v4.0.30319: Enterprise.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemCore\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemCore.dll'. Cannot find or open the PDB file. A first chance exception of type 'System.IO.IOException' occurred in PresentationFramework.dll A first chance exception of type 'System.IO.IOException' occurred in PresentationCore.dll 'Enterprise.exe' (CLR v4.0.30319: Enterprise.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Cannot find or open the PDB file. The thread 0x1954 has exited with code 259 (0x103). 

解析度:

           

感谢所有协助的人。

您不能使用RelativeSource BindingContextMenu访问TreeView.DataContext ,因为它不在主UI可视树中。 这是一个记录良好的问题,解决方案是使用ContextMeny.PlacementTarget属性和应用了Contextmenu的对象上的Tag属性将DataContext对象传递给ContextMenu

已经多次编写和重写了这些,现在,我更愿意请求你阅读我对ContextMenu.PlacementTarget的答案没有设置,不明白为什么和在datagrid中添加上下文菜单,如何获取select Item有关完整解释和代码示例的Stack Overflow 值问题。 如果您需要进一步的帮助,只需在互联网上搜索“ WPF ContextMenu DataContext ”或类似内容,您就应该找到关于这个主题的数十个教程。

ContextMenu不属于Visual Tree,因此FindAncestory数据绑定不起作用。

您可以显式设置ContextMenu上的DataContext

例如,如果这是一个选项,您可以直接在XAML中创建ViewModel / DataContext的实例。

  

或者从StaticResource获取它。

      

因为越来越多地了解C#和WPF,如果其他人正在寻找有关此问题的答案。 答案很简单。

你走错了路。 如果你需要一遍又一遍地使用“行为”并使用MVVM样式代码(或者你认为你因为它可能还没有完全存在)你需要使用他们称之为附加属性的代码。

简单的说。

  1. 创建附加的属性类。 http://dotnetbyexample.blogspot.com.au/2010/05/attached-dependency-properties-for.html应该让你入门
  2. 在下面的菜单项示例中创建模板
  3. 创建标准菜单项。
  4. 处理附加属性PropertyMetadata事件处理程序中的所有内容。