WPF:MenuItem中的按钮,关闭菜单 [英] WPF: button inside MenuItem, closing the menu

查看:319
本文介绍了WPF:MenuItem中的按钮,关闭菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MenuItem的标题内有一个Button.单击菜单项(而不是Button部分)会按预期关闭菜单,但是单击Button 不会关闭菜单,而我希望这样做.我该如何解决?

I've got a Button inside a MenuItem's header. Clicking the menu item (not the Button part) closes the menu as expected, but clicking the Button doesn't close the menu, and I want it to. How can I fix this?

更多背景:

我有两个彼此互不相同的命令,我想将它们都放在菜单中.第一个变体将在大多数时间使用,因此我真的不希望两个菜单项都占用空间,也不希望将它们都放在子菜单中.因此,我想到了在MenuItem内为辅助命令 放置一个Button的想法,就像这样:

I have two commands that are minor variants of each other, and I want to put them both in a menu. The first variant will be used most of the time, so I don't really want two menu items both taking up space, nor do I want to put them both in a submenu. So I hit on the idea of putting a Button for the secondary command inside the MenuItem, like so:

<Menu>
  <MenuItem Header="Git">
    <MenuItem Command="{Binding PrimaryCommand}">
      <MenuItem.Header>
        <StackPanel Orientation="Horizontal">
          <TextBlock VerticalAlignment="Center" Text="Rebase "/>
          <Button Content="Interactive" Focusable="False"
                  Command="{Binding SecondaryCommand}"/>
        </StackPanel>
      </MenuItem.Header>
    </MenuItem>
  </MenuItem>
</Menu>

看起来像这样:


(来源: excastle.com )


(source: excastle.com)

因此,如果我单击按钮,我将得到第二个命令;但是如果我在菜单项上的其他任何地方单击,我将得到主要命令.在某种程度上,这种方法效果很好.

So if I click on the button, I'll get the secondary command; but if I click anywhere else on the menu item, I'll get the primary command. This works well, up to a point.

问题是,无论我是单击MenuItem还是单击Button,都做出了选择,并且希望菜单关闭-但是,如果单击按钮,它不会关闭菜单.菜单保持打开状态.在某些情况下,我可以看到这实际上是有用的行为(例如,您可能希望多次单击放大/缩小按钮而不在每次单击后关闭菜单,例如Google Chrome的菜单);但是在这种情况下,我要做希望按钮关闭菜单.

The problem is, whether I'm clicking on the MenuItem or on the Button, I've made my selection and I want the menu to close -- but if I click the button, it doesn't close the menu. The menu stays open. I can see this actually being useful behavior in some cases (e.g. zoom in/out buttons that you might want to click multiple times without closing the menu after each click, as in Google Chrome's menu); but in this case I do want the button to close the menu.

万一有人对Focusable="False"感到疑惑,将其删除并不能解决问题,它会对其进行更改,以便在您第二次下拉菜单时Button会以蓝色(突出显示)边框绘制.

In case anyone wonders about that Focusable="False", removing it doesn't fix the problem, it just changes it so the Button draws with a blue (focused) border the second time you drop the menu down.

我尝试使用超链接而不是按钮,但效果相同:单击超链接不会关闭菜单.

I've tried using a Hyperlink instead of a Button, with the same effect: clicking the Hyperlink doesn't close the menu.

如何获取嵌入式按钮以关闭菜单?

推荐答案

我刚刚为菜单项添加了PreviewMouseUp事件处理程序,并在处理程序中关闭了发送方父级的子菜单:

I've just added PreviewMouseUp event handler for menu item and close submenu of sender's parent inside handler:

<MenuItem Command="{Binding PrimaryCommand}" 
          PreviewMouseUp="MenuItem_PreviewMouseUp">
     ...
</MenuItem>


private void MenuItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
    // add checks of null value by yourself 
    ((MenuItem)((MenuItem)sender).Parent).IsSubmenuOpen = false;
}

如果您要避免像我这样的背后的代码,则可以为菜单项创建行为".

If you are avoiding code behind (like me), it's possible to create Behaviour for menu item.

这篇关于WPF:MenuItem中的按钮,关闭菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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