Group Checkable Menu Items into Radio Group
This tutorial will show you how to group your checkable menu items into a radio group. This will allow you to have only one of them checked at a time.
Creating a sample RadMenu and defining the Radio Groups
Here is a sample RadMenu and one of its items has two radio groups. Notice that the Tag property of the RadMenuItem is used to store the name of the Radio Group. In this case the groups are named "1" and "2".
Handling the ItemClick Event
The next step is to handle the ItemClick event of the RadMenu. It gets fired each time an item gets clicked .
In the event handler you get the item that has been clicked and check if it is checkable or is placed in a group.
The next step is to get the sibling items of the clicked one, which are from the same group.
Getting All Sibling Items from the same Group
In order the uncheck the other items from the same group, you have to get them first. Here is a method that takes as a parameter the clicked item and returns the sibling items which are from the same group.
First you get the parent item of the clicked one and then you iterate through its Items collection. The Items collection doesn't hold the container itself, so you have to get the container of each item and check if it belongs to the same group as the clicked item. The matching containers are returned as List.
Checking only the Clicked Item
Now as the sibling items from the same group are available, the only things that's left is to unmark those of them which are different from the clicked one as unchecked.
Using this Logic with Dynamic Data
Before you continue, please, Take a look at the topic about Binding to Dynamic Data.
An entirely business object oriented approach about handling radio groups within dynamic data can be found in the online demo.
When having RadMenu with dynamic data in it, the logic remains the same, but you have to modify the data items a bit, so they can provide the needed information. You need your business object to expose several properties.
Header - indicates header text of the item. It will be bound to the Header property of the RadMenuItem.
IsCheckable - indicates whether the item is checkable. It will be bound to the IsCheckable property of the RadMenuItem.
IsSeparator - indicates whether the item is a separator. It will be bound to the IsSeparator property of the RadMenuItem.
RadioGroup - represents the radio group to which the item belongs. It will be bound to the Tag property of the RadMenuItem.
SubMenuItems - represents a collection with the sub menu items of the item. It will be bound to the ItemsSource property of the RadMenuItem.
These properties should be bound in the Style for the RadMenuItem container to its respective properties:
Next step will be to initialize the collection with the MenuItem objects which will be bound to ItemsSource property of the RadMenu.
The ViewModel should look as shown below:
Finally you need to set the created style to the ItemContainerStyle property and bind the collection to the ItemsSource property of the RadMenu.
From here on, the things work the same as in the scenario with the static items.
This will be the result: