Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This may not be what you're looking for, but you could write an extension for the <code>MenuItem</code> class that allows you to use something like the <code>GroupName</code> property of the <code>RadioButton</code> class. I slightly modified <a href="http://www.bradcunningham.net/2009/09/grouping-and-checkboxes-in-wpf.html" rel="noreferrer">this</a> handy example for similarly extending <code>ToggleButton</code> controls and reworked it a little for your situation and came up with this:</p> <pre><code>using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; namespace WpfTest { public class MenuItemExtensions : DependencyObject { public static Dictionary&lt;MenuItem, String&gt; ElementToGroupNames = new Dictionary&lt;MenuItem, String&gt;(); public static readonly DependencyProperty GroupNameProperty = DependencyProperty.RegisterAttached("GroupName", typeof(String), typeof(MenuItemExtensions), new PropertyMetadata(String.Empty, OnGroupNameChanged)); public static void SetGroupName(MenuItem element, String value) { element.SetValue(GroupNameProperty, value); } public static String GetGroupName(MenuItem element) { return element.GetValue(GroupNameProperty).ToString(); } private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //Add an entry to the group name collection var menuItem = d as MenuItem; if (menuItem != null) { String newGroupName = e.NewValue.ToString(); String oldGroupName = e.OldValue.ToString(); if (String.IsNullOrEmpty(newGroupName)) { //Removing the toggle button from grouping RemoveCheckboxFromGrouping(menuItem); } else { //Switching to a new group if (newGroupName != oldGroupName) { if (!String.IsNullOrEmpty(oldGroupName)) { //Remove the old group mapping RemoveCheckboxFromGrouping(menuItem); } ElementToGroupNames.Add(menuItem, e.NewValue.ToString()); menuItem.Checked += MenuItemChecked; } } } } private static void RemoveCheckboxFromGrouping(MenuItem checkBox) { ElementToGroupNames.Remove(checkBox); checkBox.Checked -= MenuItemChecked; } static void MenuItemChecked(object sender, RoutedEventArgs e) { var menuItem = e.OriginalSource as MenuItem; foreach (var item in ElementToGroupNames) { if (item.Key != menuItem &amp;&amp; item.Value == GetGroupName(menuItem)) { item.Key.IsChecked = false; } } } } } </code></pre> <p>Then, in the XAML, you'd write:</p> <pre><code> &lt;MenuItem x:Name="MenuItem_Root" Header="Root"&gt; &lt;MenuItem x:Name="MenuItem_Item1" YourNamespace:MenuItemExtensions.GroupName="someGroup" IsCheckable="True" Header="item1" /&gt; &lt;MenuItem x:Name="MenuItem_Item2" YourNamespace:MenuItemExtensions.GroupName="someGroup" IsCheckable="True" Header="item2"/&gt; &lt;MenuItem x:Name="MenuItem_Item3" YourNamespace:MenuItemExtensions.GroupName="someGroup" IsCheckable="True" Header="item3"/&gt; &lt;/MenuItem&gt; </code></pre> <p>It's a bit of a pain, but it offers the perk of not forcing you to write any additional procedural code (aside from the extension class, of course) to implement it.</p> <p>Credit goes to Brad Cunningham who authored the original ToggleButton solution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload