DelegateCommand没有在上下文菜单中调用 [英] DelegateCommand is not invoked in context menu
问题描述
我不能老是似乎得到这个右键弹出菜单的工作。
<预类=郎咸平的XML prettyprint-覆盖>
< ;树视图名称=NavigationPanel的ItemsSource ={结合NavigationList}/>
<风格的TargetType ={X:类型的TreeViewItem}>
< setter属性=文本菜单>
< Setter.Value>
<文本菜单的DataContext ={绑定的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型的TreeView}},路径=的DataContext}>
<菜单项标题=删除命令={绑定路径= CommandPopupClick}CommandParameter ={绑定路径=的SelectedItem}/>
<分离器/>
<菜单项标题=属性命令={绑定路径= CommandPopupClick}CommandParameter ={绑定路径=的SelectedItem}/>
< /文本菜单>
< /Setter.Value>
< /二传手>
< /样式和GT;
<预类=郎-CS prettyprint-覆盖> //委派命令
酒店的公共DelegateCommand CommandPopupClick {搞定;组; }
//分配委托指令。
CommandPopupClick =新DelegateCommand(PopupClick,CanMyCommand);
//为单击鼠标右键选择事件点击
公共无效PopupClick(对象参数)
{
VAR东西=参数; //歇在这里看到,如果事件触发
}
我可以看到弹出在它的项目删除和属性菜单。然而,当我点击其中一方不触发事件
请注意:委托指挥系统适用于一切,我不认为这是问题。
我真的不希望使用 Name.Click + =新RoutedEvent()
如果我能帮助根据要求
$ b $它。
感谢。
在调试错误b
类型'System.IO.FileNotFoundException'的第一个机会异常在mscorlib.dll $ b $发生b型'System.IO.FileNotFoundException'的第一次机会异常出现在mscorlib.dll中
'Enterprise.exe'(CLR v4.0.30319:Enterprise.exe):已加载Microsoft.GeneratedCode。
'Enterprise.exe'(CLR v4.0.30319:Enterprise.exe):已加载C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemCore\v4.0_4.0.0 .0__b77a5c561934e089\PresentationFramework-SystemCore.dll。无法找到或打开PDB文件。
型'System.IO.IOException'的第一次机会异常出现在PresentationFramework.dll
型'System.IO.IOException'的第一次机会异常出现在PresentationCore.dll中
'企业.EXE(CLR v4.0.30319:Enterprise.exe):已加载C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll 。无法找到或打开PDB文件。
线程0x1954已退出,代码为259(0x103)。
解析:
< setter属性=文本菜单>
< Setter.Value>
<文本菜单的DataContext ={结合PlacementTarget.Tag,的RelativeSource = {自我的RelativeSource}}>
<菜单项标题=删除
命令={结合CommandPopupClick}
CommandParameter ={结合PlacementTarget.DataContext,的RelativeSource = {的RelativeSource AncestorType =文本菜单}
CommandTarget ={结合PlacementTarget,的RelativeSource = {自我的RelativeSource}}/>
< /文本菜单>
< /Setter.Value>
< /二传手>
< /样式和GT;
<树视图名称=NavigationPanel的ItemsSource ={结合NavigationList}标签={绑定路径= DataContext的,的ElementName =主}/>
感谢您所有谁协助。
您不能使用的RelativeSource绑定
访问 TreeView.DataContext
从文本菜单
,因为它的不的主UI的视觉树。这是一个非常有据可查的问题,解决的办法是通过对文本菜单
使用<$传递的DataContext
对象C $ C,它有文本菜单的对象> ContextMeny.PlacementTarget
属性和标签
属性适用于它。
在已经编写并重新写了很多次,如今,我更喜欢请您阅读我的答案对的 ContextMenu.PlacementTarget是没有得到设置,不知道为什么一>和添加在DataGrid中,如何在这里对堆栈溢出得到选择项目价值问题进行全面的解释和代码示例上下文菜单。如果您需要进一步的帮助,只需搜索的 WPF文本菜单的DataContext 的'或类似的东西,你应该找到数十这个确切的主题教程。
互联网
I can`t seem to get this right click popup menu to work.
<TreeView Name="NavigationPanel" ItemsSource="{Binding NavigationList}" />
<Style TargetType="{x:Type TreeViewItem}" >
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}, Path=DataContext}">
<MenuItem Header="Delete" Command="{Binding Path=CommandPopupClick}" CommandParameter="{Binding Path=SelectedItem}"/>
<Separator />
<MenuItem Header="Properties" Command="{Binding Path=CommandPopupClick}" CommandParameter="{Binding Path=SelectedItem}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
//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
}
I can see the popup menu with the items 'Delete' and 'Properties' in it. However, when I click either one of them it doesn't fire the event.
NOTE: the delegate command system works for everything else, I don't think that's the issue.
I really do not want to use Name.Click += new RoutedEvent()
if I can help it.
Thanks.
Errors in Debug as requested
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).
Resolution:
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Delete"
Command="{Binding CommandPopupClick}"
CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
<TreeView Name="NavigationPanel" ItemsSource="{Binding NavigationList}" Tag="{Binding Path=DataContext, ElementName=Main}"/>
Thank you for all who assisted.
You can't use a RelativeSource Binding
to access the TreeView.DataContext
from the ContextMenu
because it is not in the main UI visual tree. This is a very well documented problem and the solution is to pass the DataContext
object through to the ContextMenu
using the ContextMeny.PlacementTarget
property and the Tag
property on the object that has the Contextmenu
applied to it.
Having already written and re-written about this many times, nowadays, I much prefer to request that you read my answers to the ContextMenu.PlacementTarget is not getting set, no idea why and Add context menu in datagrid, how to get the select Item value questions here on Stack Overflow for full explanations and code examples. If you need further help, just search the internet for 'WPF ContextMenu DataContext' or something similar and you should find dozens of tutorials on this exact topic.
这篇关于DelegateCommand没有在上下文菜单中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!