简体   繁体   English

右键单击后,禁用的上下文菜单不会消失

[英]Disabled context menu won't disappear after rightclick

I have a ContextMenu as part of a TabControl such as: 我有一个ContextMenu作为TabControl一部分,例如:

<TabControl Name="MyTabControl">
    <TabControl.ContextMenu>
        <ContextMenu Name="MyContextMenu" IsEnabled="False" StaysOpen="True">
            <MenuItem Header="Item1"></MenuItem>
            ...
        </ContextMenu>
    </TabControl.ContextMenu>
</TabControl>

As you can see, the menu is disabled. 如您所见,菜单已禁用。 It will be enabled later in the program, for now however, it should remain in its disabled state. 它将在程序的后面启用,但是现在,它应该保持禁用状态。 The problem is, when I right-click the TabControl and the disabled menu shows, it simply stays where it was first opened, no other right-click will move it, nor will a left-click somewhere make it disappear. 问题是,当我右键单击TabControl并显示禁用的菜单时,它只是停留在首次打开的位置,没有其他右键单击将其移动,也不会在某个位置单击鼠标左键使其消失。

The only way to get rid of it, would be either by enabling it and then right/left-clicking, or by using MyContextMenu.Visibility = Visibility.Collapsed/Hidden; 摆脱它的唯一方法是,先启用它,然后单击MyContextMenu.Visibility = Visibility.Collapsed/Hidden;右键/左键,或者使用MyContextMenu.Visibility = Visibility.Collapsed/Hidden;

I tried setting the StaysOpen -property to False . 我尝试将StaysOpen设置为False Then the menu will open once in its disabled state. 然后,菜单将在其禁用状态下打开一次。 After left-clicking disappear and then not appear again even if it gets enabled. 左键单击后消失,即使启用了它也不会再次出现。

The only way around it could be changing the StaysOpen -property along with the IsEnabled -property, but it is a bit weird that the menu opens exactly once in its disabled state and then not anymore. 解决此问题的唯一方法可能是更改StaysOpen属性以及IsEnabled属性,但是菜单在禁用状态下仅打开一次,然后再不打开,这StaysOpen

I hope anybody could explain to me, why exactly a disabled menu won't close and the property StaysOpen at false makes it open exactly once, because it seems like a weird behaviour to me. 我希望任何人都可以向我解释,为什么完全禁用的菜单不会关闭,而属性StaysOpen为false则StaysOpen将其打开一次,因为在我看来这是一种奇怪的行为。

It seems that the behaviour of context menu items is quite strange - what you've described is in .Net 4.0 and if you target yor app to .Net 3.5 for instance you will notice the opposite behaviour - you can't make the menu stay opened if the single item is disabled, it just disappears immediately after it shows on right click. 似乎上下文菜单项的行为非常奇怪-您所描述的是在.Net 4.0中,如果您将应用定位到.Net 3.5,例如,您会注意到相反的行为-您无法使菜单停留如果禁用了单个项目,则打开,在右键单击显示后它会立即消失。

However I think the preferrable way to manage the enabled state of a context menu item (and also the OnClick action it should do) is by a Command . 但是,我认为管理上下文菜单项的启用状态(以及它应执行的OnClick操作)的首选方法是通过Command

First you should specify a datacontext for your view, let's say it is the class ViewModel.cs . 首先,您应该为视图指定一个数据上下文,假设它是ViewModel.cs类。 Then create a command by implementing the ICommand interface, something like this: 然后通过实现ICommand接口创建命令,如下所示:

public class MyCommand : ICommand

And then you have a CanExecute method which does exactly what it's name says - decides if the command can be executed or not. 然后,您有一个CanExecute方法,其功能完全符合其名称所说的-决定是否可以执行命令。 And the enabled state of the menu item also depends on the return value of this method. 菜单项的启用状态还取决于此方法的返回值。 So you can keep it returning false as long as you need in and the menu will behave correctly and the menu item will be disabled. 因此,只要您需要,就可以保持它返回false ,菜单将正确运行并且菜单项将被禁用。 Then when you want you can make it return true (by some logic in the method itself) and you'll have again a properly working context menu with enabled menu item. 然后,如果需要,可以使它返回true (通过方法本身的某种逻辑),然后您将再次具有一个启用菜单项的正常工作上下文菜单。

If you want to disable the entire menu, use the same approach but for the menu. 如果要禁用整个菜单,请对菜单使用相同的方法。

And if you need to make all the items in the menu disabled (which I think is different from the entire menu), then you can use something like this: 而且,如果您需要禁用菜单中的所有项目(我认为这与整个菜单有所不同),则可以使用以下方法:

<TabControl Name="MyTabControl" Background="Green">
            <TabControl.ContextMenu>
                <ContextMenu Name="MyContextMenu"  StaysOpen="True" ItemsSource="{Binding  Items}">
                    <ContextMenu.ItemTemplate>
                        <DataTemplate >
                            <MenuItem Header="{Binding Header}"  IsEnabled="False" />
                        </DataTemplate>
                    </ContextMenu.ItemTemplate>
                </ContextMenu>
            </TabControl.ContextMenu>
        </TabControl> 

Here your ItemsSource ( Items ) is for instance List<MenuItem> , and MenuItem itselw would be your Model (thinking in MVVM) for each of your menu items, which should have property Header that contains the header you want to see in each menu item. 在这里,您的ItemsSourceItems )例如是List<MenuItem> ,而MenuItem itselw将是每个菜单项的模型(在MVVM中考虑),该菜单项应具有属性Header ,该属性包含要在每个菜单项中看到的标题。 Instead of setting the IsEnabled to false as in my dummy example, you can bind it to a property in order to have control on when it must be true and false. 可以像在我的虚拟示例中那样将IsEnabled设置为false ,而不是将其绑定到属性,以便控制何时必须为true和false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM