0

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?

Will
  • 3,413
  • 7
  • 50
  • 107
  • When you enter the text field, there is more going on than just a click. An element other than the menu is getting full keyboard capture/focus, which likely triggers a totally different chain of events. You might try intercepting changes to IsSubMenuOpen and canceling or reversing them, but my attempts at that resulted in unacceptable behavior. Though it did, in fact, stay open. – Daniel Nov 20 '16 at 22:45
  • Something else, you could try extracting the MenuItem's template to a resource and replacing this (in popup) with a variable you control: IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" – Daniel Nov 20 '16 at 22:48
  • Yep, even forcing the entire tree of menus to remain open still results in unacceptable behavior. Some part of the menu is forcefully ripping focus away from the text field. This is a bit more than I am willing to investigate on a Sunday, but I wish you good luck. I hope the above has helped you in some way. – Daniel Nov 20 '16 at 23:04
  • I thank you. I think that the best way to do this would be to extend the MenuItem control and drop the control into it. Then setting the inherited StaysOpenOnClicked property to "True" would actually cause it to honor that property. I will attempt this and post the results here. – Will Nov 21 '16 at 00:54

2 Answers2

1

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>
Will
  • 3,413
  • 7
  • 50
  • 107
-1

you can make use of StaysOpenOnClick Property to achieve this

venkatesan r
  • 249
  • 3
  • 15
  • Try it. It doesn't work. You have to create your content within the header of a menu item and place that menu item within the menu item you want to stay open. Setting the property of the embedded menu item to True will then accomplish the desired effect, as per my answer above. – Will Nov 22 '16 at 14:58