namg_engr
namg_engr

Reputation: 359

WPF menu will not close when radiobutton group item is selected

I created a menuitem resource style that assists in showing a group of mutually exclusive selectable radio button items. When one of the radio button items is selected the menu will not close. I tried using StaysOpenOnClick but that doesn't seem to work. How can I get the menu to close when one of the radio button menu items is selected?

XAML:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="200" Width="200">
<Grid>
    <Menu>
        <MenuItem Header="Menu" StaysOpenOnClick="False" >
            <MenuItem.Resources>
                <Style x:Key="GroupStyle1" TargetType="{x:Type RadioButton}">
                    <Setter Property="GroupName" Value="OptionGroup1"/>
                </Style>
            </MenuItem.Resources>
            <MenuItem StaysOpenOnClick="False">
                <MenuItem.Template>
                    <ControlTemplate>
                        <RadioButton
                                 Content="Radio1" 
                                 Style="{StaticResource GroupStyle1}"/>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>
            <MenuItem StaysOpenOnClick="False">
                <MenuItem.Template>
                    <ControlTemplate>
                        <RadioButton
                                  Content="Radio2" 
                                  Style="{StaticResource GroupStyle1}"/>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>
            <MenuItem StaysOpenOnClick="False">
                <MenuItem.Template>
                    <ControlTemplate>
                        <RadioButton
                                  Content="Radio3" 
                                  Style="{StaticResource GroupStyle1}"/>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>
        </MenuItem>
    </Menu>
</Grid>

Upvotes: 0

Views: 173

Answers (1)

Nik
Nik

Reputation: 1870

I usually try not to use code behind with WPF, but for things like this, I think it may be justified. I'm sure if you think long enough you can write a whole bunch of xaml that can do this, but here's a quick and slightly dirty way of doing so:

  1. Name the MenuItem
  2. Add an event handler to the style, and use that to close the menu in the code behind. (See the EventSetter inside the style)

Xaml

<Grid>
    <Menu>
        <MenuItem x:Name="menuItem" Header="Menu">
            <MenuItem.Resources>
                <Style x:Key="GroupStyle1" TargetType="{x:Type RadioButton}">
                    <Setter Property="GroupName" Value="OptionGroup1"/>
                    <EventSetter Event="Checked" Handler="RadioButton_Checked"/> <!--Add this-->
                </Style>
            </MenuItem.Resources>
...

Code Behind

private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
    menuItem.IsSubmenuOpen = false;
}

Upvotes: 1

Related Questions