WPF Adaptable ContextMenu [英] WPF Adaptable ContextMenu

查看:116
本文介绍了WPF Adaptable ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个包含元素和上下文菜单的列表框。

但是我想只在有>时显示一些MenuItems在ListBox中选择了1个项目。



我的代码目前是:



Hello,

I have a listbox with elements and a contextmenu on it.
But I want to display some MenuItems only if there are > 1 items selected in the ListBox.

My code is currently this:

<ListBox x:Name="lsbChampionsInGroup" Margin="10,220,5,11" SelectionMode="Multiple" HorizontalAlignment="Left" Width="160" RenderTransformOrigin="0.5,0.5">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseRightButtonDown" Handler="OnListBoxItemPreviewMouseRightButtonDown" />
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.ContextMenu>
                <ContextMenu Name="cmChampionsInGroup">
                    <MenuItem Header="Unselect all" Click="UnselectAllChampions" />
                    <Separator Background="CornflowerBlue" />
                    <MenuItem Header="Remove" Click="btnRemoveChampionsFromCurrentGroup_Click"/>
                    <Separator Background="CornflowerBlue" />
                    <MenuItem Header="Copy" Click="CopySelectedChampionsInCurrentGroup" />
                    <MenuItem Header="Paste" Click="PasteSelectedChampions" />
                </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>
</pre>





ItemContainerStyle确保如果用户右键单击该项目,我不会取消选择该项目。



我必须为3个ListBox做这个。



真的

Bert



The ItemContainerStyle makes sure I do not unselect an item if a user right-clicks on it.

I would have to do this for 3 ListBoxes.

Sincerly
Bert

推荐答案

你可以处理事件 System.Windows.Controls.ContextMenu.ContextMenuOpening

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.contextmenuopening%28v=vs.110%29 .aspx [ ^ ]。



在此活动的处理程序中,检查您的情况并调整菜单项的可见性问题:

http://msdn.microsoft.com/en-us/library/system.windows.uielement.isvisible(v=vs.110).aspx [ ^ ]。



在其他情况下,此事件处理程序可以修改上下文菜单本身(其项目)的内容:添加/删除菜单项,更改其外观,启用/禁用状态(http://msdn.microsoft.com/en-us/library/system.windows。 uielement.isenabled(v = vs.110).aspx [ ^ ]),依此类推,具体取决于上下文的状态:应用程序或拥有上下文菜单的UI元素。 />


-SA
You can handle the event System.Windows.Controls.ContextMenu.ContextMenuOpening:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.contextmenuopening%28v=vs.110%29.aspx[^].

In the handler of this event, check up your condition and adjust the visibility of the menu items in question:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.isvisible(v=vs.110).aspx[^].

In other cases, this event handler is the point where you can modify the content of the context menu itself (its items): add/remove menu items, change their appearance, enable/disable status (http://msdn.microsoft.com/en-us/library/system.windows.uielement.isenabled(v=vs.110).aspx[^]), and so on, depending on the status of the context: the application or the UI element owning the context menu.

—SA


这篇关于WPF Adaptable ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆