How do You Add Radio Buttons To Menu Items?
Asked Answered
I

2

15

I would like to add radio buttons to my menu items.

I have seen a few answers where people were making radio buttons as the menu items. But I want my menu items to have the proper radio button that can be easily done in Winforms.

To make sure I don't get the same answers as other people, here is a picture of what the menu items' radio buttons should look like:

enter image description here

And by the way, I am using C#, WPF.

Iormina answered 20/2, 2013 at 12:21 Comment(3)
Have you seen this? #6253645Sling
@Neil Mitchell: In the case when you need to support other themes such as Luna, Aero, Classic etc., probably will need to be created for each theme style. That is needed for each theme carry with it a style for each theme. Does that suit you? I mean, it's better to focus on the functionality and radio buttons to make at least a common style for him. Then, if you need to be able to make it fit your style. If I or anyone else will give you a solution for the one style and radio button, it will come to you?Hairspring
@AnatoliyNikolaev This question is only about the style - writing the functionality is pretty trivial. And what I want is a way to leverage what is already in the themes, not write new style elements. Perhaps it is the case that WPF doesn't define styles for radio menu buttons, or perhaps I just can't get the style out properly. Certainly the underlying OS (not WPF) does define radio menu styles.Staffer
H
17

Edited Template

 <Window.Resources>        
    <Style x:Key="{x:Type MenuItem}" TargetType="MenuItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type MenuItem}">
                    <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <Grid VerticalAlignment="Center">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                            <RadioButton Margin="3" IsChecked="{TemplateBinding IsChecked}" VerticalAlignment="Center" x:Name="GlyphPanel" Visibility="Collapsed"/>
                            <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom">
                                <Border x:Name="SubMenuBorder" BorderBrush="#FF999999" BorderThickness="1" Background="#FFF0F0F0" Padding="2">
                                    <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                        <Grid RenderOptions.ClearTypeHint="Enabled">
                                            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                                <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                                            </Canvas>
                                            <Rectangle Fill="#FFD7D7D7" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/>
                                            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                                        </Grid>
                                    </ScrollViewer>
                                </Border>
                            </Popup>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                            <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
                        </Trigger>
                        <Trigger Property="Icon" Value="{x:Null}">
                            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="IsHighlighted" Value="True">
                            <Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>
                            <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
                            <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
                            <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ItemcontainerStyle" TargetType="MenuItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type MenuItem}">
                    <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <Grid Margin="-1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
                                <ColumnDefinition Width="13"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="30"/>
                                <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
                                <ColumnDefinition Width="20"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                            <Border x:Name="GlyphPanel" BorderBrush="#FF26A0DA" BorderThickness="1" Background="#3D26A0DA" ClipToBounds="False" HorizontalAlignment="Center" Height="22" Margin="-1,0,0,0" Visibility="Hidden" VerticalAlignment="Center" Width="22">
                                <RadioButton x:Name="Glyph" IsChecked="{TemplateBinding IsChecked}" GroupName="a"  VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                            <ContentPresenter x:Name="menuHeaderContainer" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="2" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                            <TextBlock x:Name="menuGestureText" Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Icon" Value="{x:Null}">
                            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                        </Trigger>
                        <Trigger  Property="IsChecked" Value="True">
                            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                        </Trigger>
                        <Trigger SourceName="Glyph"  Property="IsChecked" Value="False">
                            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Hidden"/>
                            <Setter Property="Visibility" TargetName="Glyph" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="IsHighlighted" Value="True">
                            <Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>
                            <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsHighlighted" Value="True"/>
                                <Condition Property="IsEnabled" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>
                            <Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

MainWindow.xaml:

  <Menu>
        <MenuItem Header="File" ItemContainerStyle="{StaticResource ItemcontainerStyle}">
            <MenuItem IsCheckable="True" Header="Example Menu Item"/>
            <MenuItem IsCheckable="True" Header="Example Menu Item"/>
        </MenuItem>
    </Menu>

App.xaml

<Application x:Class="MenuItemTemplate.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>        
    <!--Luna-->
    <ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0,
    Culture=neutral, PublicKeyToken=31bf3856ad364e35,
    ProcessorArchitecture=MSIL;component/themes/luna.normalcolor.xaml" />

    <!--Aero-->
    <!--<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0,
        Culture=neutral, PublicKeyToken=31bf3856ad364e35,
        ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"/>-->

    <!--Classic-->
    <!--<ResourceDictionary Source="/PresentationFramework.Classic, Version=3.0.0.0,
        Culture=neutral, PublicKeyToken=31bf3856ad364e35,
        ProcessorArchitecture=MSIL;component/themes/classic.xaml" />-->
</Application.Resources>

OutPut

enter image description here

Houchens answered 9/3, 2014 at 14:3 Comment(1)
after clicking on the menu a few times it takes two clicks to select the unselected menu item.Countersink
E
2

You can change the look of a Control by changing its Template-property:

            <MenuItem>
                <MenuItem.Template>
                    <ControlTemplate>
                        <RadioButton>Radio</RadioButton>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>

EDIT: Use a RadioButton as MenuItem-Icon, to get the look which is shown in the picture:

                <MenuItem Header="Hallo">
                    <MenuItem.Icon>
                        <RadioButton/>
                    </MenuItem.Icon>
                </MenuItem>
Epley answered 20/2, 2013 at 12:37 Comment(1)
I have used to code that you have, but it is making a radio button instead of having that square box with a circle in it, on the right of the menu item's text. Can you please give some code that will make the same radio button with a circle in the square, as shown in my picture?Iormina

© 2022 - 2024 — McMap. All rights reserved.