locked
Reg MenuItem RRS feed

  • Question

  •  

    Hi,

     

    How to group the checkable menu items in WPF windows application. Say in View menu i am having Icon,Details,List checkable submenu. I have to group that in to single unit. So when i selecting Details menu others should unchecked automatically.

     

    Any reference...

     

     

    Regards,

    Arun.M

     

     

    Tuesday, May 27, 2008 6:58 AM

Answers

  • Hi Arun,

     

    There isnt a built-in way to do this, but it's quite simple to do using the same click event handler for all the menus that need mutual exclusion.  Here's how I did it:

     

    Xaml:

     

    Code Snippet

    <Menu>

    <MenuItem Header="Only one of these can be selected">

    <MenuItem Header="MutexItem 1" Click="MenuItem_Click" IsCheckable="True" ></< FONT>MenuItem>

    <MenuItem Header="MutexItem 2" Click="MenuItem_Click" IsCheckable="True" ></< FONT>MenuItem>

    <MenuItem Header="MutexItem 3" Click="MenuItem_Click" IsCheckable="True" ></< FONT>MenuItem>

    </< FONT>MenuItem>

    </< FONT>Menu>

     

     


    Code behind:

     

    Code Snippet

    private MenuItem checkedItem;

     

    private void MenuItem_Click(object sender, RoutedEventArgs e)

    {

    if (checkedItem != null)

    {

    checkedItem.IsChecked = false;

    }

    checkedItem = (MenuItem) sender;

    checkedItem.IsChecked = true;

    }

     

     

     

    (Mad props to Chuck Petzold for the suggestion in his book about this...)

     

    Hope this helps,

    Matt

     

    • Marked as answer by Marco Zhou Monday, June 2, 2008 9:50 AM
    Wednesday, May 28, 2008 5:15 PM