Search code examples
c#wpfxamlmenuitemxceed

How can I keep a menu submenu item open?


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.

Users will not like that.

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?


Solution

  • I have figured out a solution. It is sort of a terribly hacky workaround, but it does the job quite well :

    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>