【发布时间】:2014-07-29 08:13:28
【问题描述】:
我似乎无法让这个右键弹出菜单工作。
<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
}
我可以看到包含“删除”和“属性”项的弹出菜单。但是,当我单击其中任何一个时,它都不会触发事件。
注意:委托命令系统适用于其他一切,我认为这不是问题。
如果可以的话,我真的不想使用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).
分辨率:
<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}"/>
感谢所有提供帮助的人。
【问题讨论】:
-
绑定有数据错误吗?调试时检查输出窗格。
-
按要求添加错误代码。
-
很遗憾,它们都不是数据绑定错误,请查看打开上下文菜单或单击菜单时是否出现任何数据错误。
-
你可以尝试在
MenuItem上设置DataContext而不是父ContextMenu吗? -
右键单击该项目并打开上下文菜单时没有错误。当我单击菜单项时,输出窗口中没有输出
标签: c# wpf xaml contextmenu