Click here to Skip to main content
Click here to Skip to main content
Go to top

Binding Radio Buttons to a Single Property

, 11 Aug 2014
Rate this:
Please Sign up or sign in to vote.
Describes the approach to bind a group of radio buttons to a single property, regardless of the property type (enum, bool, int, string).

Introduction

While building a user interface for things like an options dialog, you may want to represent a Boolean, enumeration, and sometimes an integer or string property with a group of radio buttons.

Background

I did some research and found quite a few articles on how to do this using a converter. However, most of the solutions were cumbersome, built for a specific type and had a long thread of questions/issues.

Here is the most comprehensive post that I found: http://stackoverflow.com/a/2908885/2005727. This solution is inspired by this post, and I wanted to go further and provide a complete code sample.

The Solution

You need to use a converter parameter to specify which value a radio button corresponds to. If your property equals to this value, this button will be checked, and other buttons unchecked. This solution works regardless whether you set the GroupName property or not, though you should normally set it.

Here is an example on how to do this in XAML for a Boolean property:

<RadioButton GroupName="BooleanGroup" 
    IsChecked="{Binding BoolProperty, Converter={StaticResource RadioButtonCheckedConverter}, 
    ConverterParameter={x:Static src:MainWindow.BooleanTrue}}">
</RadioButton>

<RadioButton GroupName="BooleanGroup" 
    IsChecked="{Binding BoolProperty, Converter={StaticResource RadioButtonCheckedConverter}, 
    ConverterParameter={x:Static src:MainWindow.BooleanFalse}}">
</RadioButton>

Note that I defined two static Boolean members in my class so I can use the static binding in XAML. Alternatively, you can set the converter parameter in a less concise way as described here: http://stackoverflow.com/questions/3978937/how-to-pass-an-integer-as-converterparameter.

Another interesting approach is to use resources instead of the static binding, as discussed here:  http://stackoverflow.com/questions/4997446/boolean-commandparameter-in-xaml.

<Application.Resources>
 <system:Boolean x:Key="True">True</system:Boolean>
 <system:Boolean x:Key="False">False</system:Boolean>
</Application.Resources>

<RadioButton GroupName="BooleanGroup"
    IsChecked="{Binding BoolProperty, Converter={StaticResource RadioButtonCheckedConverter},
    ConverterParameter={StaticResource True}}">
</RadioButton>

<RadioButton GroupName="BooleanGroup"
    IsChecked="{Binding BoolProperty, Converter={StaticResource RadioButtonCheckedConverter},
    ConverterParameter={StaticResource False}}">
</RadioButton>

Here is an example for an enumeration:

<RadioButton GroupName="EnumGroup"
    IsChecked="{Binding EnumProperty, Converter={StaticResource RadioButtonCheckedConverter}, 
    ConverterParameter={x:Static src:TestEnum.Option1}}">
</RadioButton>

<RadioButton GroupName="EnumGroup"  
    IsChecked="{Binding EnumProperty, Converter={StaticResource RadioButtonCheckedConverter}, 
    ConverterParameter={x:Static src:TestEnum.Option2}}">
</RadioButton>

<RadioButton GroupName="EnumGroup"  
    IsChecked="{Binding EnumProperty, Converter={StaticResource RadioButtonCheckedConverter}, 
    ConverterParameter={x:Static src:TestEnum.Option3}}">
</RadioButton>

The same approach can be applied for string and integer properties. Here is the code of the converter, which is the same regardless of the property type:

public class RadioButtonCheckedConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
        System.Globalization.CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
        System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? parameter : Binding.DoNothing;
    }
}

Please note that this solution will not work for flag enumeration properties (e.g. you want to bind a set of check boxes to a single property). You can find a solution for flag enumeration types in the article referenced in the background section above.

History

Initial post.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Volodymyr Trubachov
Software Developer (Senior)
United States United States
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
SuggestionSome "basics" missing PinmemberMember 874788419mins ago 
GeneralMy vote of 4 PinmemberMahBulgaria18-Aug-14 2:53 
GeneralMy vote of 5 PinmemberTheBigData6-Aug-14 8:12 
QuestionVery good article Volodya! PinprofessionalVolynsky Alex4-Feb-14 9:25 
QuestionGood share PinmemberMadhava Verma Dantuluri4-Feb-14 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 11 Aug 2014
Article Copyright 2014 by Volodymyr Trubachov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid