如何让菜单子菜单项保持打开状态? [英] How can I keep a menu submenu item open?
问题描述
我正在尝试创建一个有点复杂的菜单项,它允许用户创建一个新类.我遇到的问题是,当我单击数字上下(来自 xceed 工具包)菜单项关闭时,即使属性 StaysOpenOnClick
设置为 true.
I'm trying to create a somewhat complex menu item that would allow a user to create a new class. The problem into which I am running is that when I click on a numeric up-down ( from the xceed toolkit ) that the menu item closes, even with the property StaysOpenOnClick
set to true.
用户不会喜欢那样的.
要重现,请创建一个 WPF 项目并通过 NuGet 添加扩展 WPF 工具包,然后将以下代码放入您的主窗口类中:
To reproduce, create a WPF project and add the Extended WPF Toolkit through NuGet, then drop the following code into your mainwindow class :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WhyDoesMyMenuItemCloseWhenClicked"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
x:Class="WhyDoesMyMenuItemCloseWhenClicked.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="21"/>
<RowDefinition />
</Grid.RowDefinitions>
<Menu FontWeight="Bold">
<MenuItem Header="_File">
<MenuItem StaysOpenOnClick="True">
<Grid Height="50" Width="50">
<xctk:IntegerUpDown/>
</Grid>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
当我上下单击整数的文本字段时,菜单关闭.
When I click the text field of the integer up-down, the menu closes.
为什么会一直这样?我怎样才能让它不发生?
Why does that keep happening? How can I make it NOT happen?
推荐答案
我想出了一个解决方案.这是一种非常糟糕的解决方法,但它可以很好地完成工作:
I have figured out a solution. It is sort of a terribly hacky workaround, but it does the job quite well :
变化是您在 MenuItem
中创建了一个 MenuItem
.然后您在子 MenuItem
的 MenuItem.Header
属性中定义您的控件,并设置该 MenuItem
的 StaysOpenOnClick
属性为 true.
The change is that you create a MenuItem
within the MenuItem
. Then you define your control within the sub MenuItem
's MenuItem.Header
property, and set that MenuItem
's StaysOpenOnClick
property to true.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WhyDoesMyMenuItemCloseWhenClicked"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
x:Class="WhyDoesMyMenuItemCloseWhenClicked.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="21"/>
<RowDefinition />
</Grid.RowDefinitions>
<Menu FontWeight="Bold">
<MenuItem Header="_File" StaysOpenOnClick="True">
<MenuItem Header="_StaysOpenOnClick">
<MenuItem StaysOpenOnClick="True">
<MenuItem.Header>
<xctk:IntegerUpDown/>
</MenuItem.Header>
</MenuItem>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
这篇关于如何让菜单子菜单项保持打开状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!