如何访问UserControl Resources + Infragistics Control(WPF)中的上下文菜单,C#

时间:2022-09-13 08:31:57

I have a context menu situated inside a User Control Resource.

我有一个位于用户控制资源内的上下文菜单。

<UserControl.Resources>

    <ContextMenu x:Key="Menu1">   

        <MenuItem Header="View/Edit Contact" Command="{Binding SearchCommand}" CommandParameter="editcontact"/>
        <MenuItem Header="View/Edit Company" Command="{Binding SearchCommand}" CommandParameter="editprimarycompany"/>         

    </ContextMenu>


</UserControl.Resources>

This resource is bound to a WPF grid.

此资源绑定到WPF网格。

Now, after the grid is filled with data, I am right clicking on the grid. And I am able to find the context menu.

现在,在网格填满数据后,我右键单击网格。我能够找到上下文菜单。

The problem is how can I know at runtime which menu item has been clicked?

问题是如何在运行时知道单击了哪个菜单项?

I have tried with this

我试过这个

var t = this.TryFindResource("Menu1") as Style;

var t = this.TryFindResource(“Menu1”)作为Style;

in the grid's SelectedItemsChanged event but it is null.

在网格的SelectedItemsChanged事件中,但它为null。

Please help me and also tell me in which event of grid should I will be able to acess this!

请帮助我,并告诉我应该在哪个网格事件中进行此操作!

Thanks in advance.

提前致谢。

I am using C#,WPF,Infragistics Control(WPF)

我正在使用C#,WPF,Infragistics Control(WPF)

1 个解决方案

#1


Well first of all, why would you cast the Menu1 ContextMenu as a Style? That will always return null because Menu1 is not a Style. Cast it as a ContextMenu.

首先,为什么要将Menu1 ContextMenu转换为样式?这将始终返回null,因为Menu1不是Style。将其转换为ContextMenu。

Secondly, it appears you already have everything in place to determine which menu item has been clicked. You have passed a unique string in the CommandParameter, which you can check at run-time.

其次,您似乎已经掌握了所有内容以确定单击了哪个菜单项。您已在CommandParameter中传递了一个唯一的字符串,您可以在运行时检查该字符串。

#1


Well first of all, why would you cast the Menu1 ContextMenu as a Style? That will always return null because Menu1 is not a Style. Cast it as a ContextMenu.

首先,为什么要将Menu1 ContextMenu转换为样式?这将始终返回null,因为Menu1不是Style。将其转换为ContextMenu。

Secondly, it appears you already have everything in place to determine which menu item has been clicked. You have passed a unique string in the CommandParameter, which you can check at run-time.

其次,您似乎已经掌握了所有内容以确定单击了哪个菜单项。您已在CommandParameter中传递了一个唯一的字符串,您可以在运行时检查该字符串。