Using the check box or any other control with MVVM is comparatively easy, because we usually bind one control with one property. But radio button is an interesting control, here we bind more than one control with one property.
When we are creating a radio button, we can defined different groups and all the radio button in one group work independent of other. And we usually bind one property to all radio button control in one group.
At our first step we bind all the radio button controls in one group to one variable, but the problem is how do we know which radio button is clicked. Let’s say we have a radio button group of direction. We have an enumerator for it.
Which radio button is clicked we can achieve this in different ways. We can use the Converter and Converter Parameter to identify the radio button. We can even make that converter as a markup extension, so we even don’t have to create an object of it in XAML.
Here is a code of our simple enum to boolean convertor markup extension.
And we can use this markup extension with radio button in this way.
Rest of the things are very easy. We can create a enum Direction type property in our view Model to use radio button in MVVM.
Here is a complete C# code of the project.
And here is complete XAML code of the project
This is an output of the program.