Click here to Skip to main content
15,867,141 members
Articles / Desktop Programming / WPF
Tip/Trick

Binding Radio Buttons to a Single Property

Rate me:
Please Sign up or sign in to vote.
4.93/5 (32 votes)
11 Aug 2014Ms-PL2 min read 163.4K   4.3K   30   21
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:

XML
<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.

XML
<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:

XML
<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:

C#
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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseGood article Pin
mikey80319-Oct-22 7:02
mikey80319-Oct-22 7:02 
Generalnewbie to this Pin
mikey80328-May-20 5:19
mikey80328-May-20 5:19 
QuestionDefinitely the best answer out there! Pin
Zombies with Coffee, LLC12-Jun-19 2:30
professionalZombies with Coffee, LLC12-Jun-19 2:30 
Thanks for giving me an absolutely fantastic answer on radiobutton bindings. I've read many articles and was wondering when I'd find one that properly used a converter.
SuggestionIf only two radiobuttons are desired Pin
Mike Azazel20-Dec-18 0:19
Mike Azazel20-Dec-18 0:19 
GeneralMy vote of 5 Pin
#realJSOP30-Sep-17 7:33
mve#realJSOP30-Sep-17 7:33 
QuestionMy vote of 5! Pin
wbankovitch29-Sep-17 9:34
wbankovitch29-Sep-17 9:34 
QuestionContent same as ConverterParameter Pin
Member 1280563622-Sep-17 13:39
Member 1280563622-Sep-17 13:39 
QuestionThanks, neat and tidy and works Pin
AvidFan28-Apr-17 4:20
AvidFan28-Apr-17 4:20 
QuestionI think there is a more elegant solution if you are open to it... Pin
pirit416-Oct-15 6:25
pirit416-Oct-15 6:25 
AnswerRe: I think there is a more elegant solution if you are open to it... Pin
super-e-17-Dec-15 23:28
super-e-17-Dec-15 23:28 
QuestionProblem when the source property is not an Enum Pin
DeMultiplexer 14-Jun-15 3:25
DeMultiplexer 14-Jun-15 3:25 
QuestionThank you Pin
lizaoreo21-May-15 13:49
lizaoreo21-May-15 13:49 
QuestionHow can I set the Default Value to Nothing Pin
Member 1151530910-Mar-15 22:56
Member 1151530910-Mar-15 22:56 
AnswerRe: How can I set the Default Value to Nothing Pin
steveski7422-Aug-19 19:08
steveski7422-Aug-19 19:08 
GeneralMy vote of 5 Pin
Member 938177121-Jan-15 15:24
Member 938177121-Jan-15 15:24 
SuggestionSome "basics" missing Pin
Member 87478841-Oct-14 10:51
Member 87478841-Oct-14 10:51 
GeneralRe: Some "basics" missing Pin
imac200415-Dec-16 5:46
imac200415-Dec-16 5:46 
GeneralMy vote of 4 Pin
MahBulgaria18-Aug-14 2:53
MahBulgaria18-Aug-14 2:53 
GeneralMy vote of 5 Pin
TheBigData6-Aug-14 8:12
TheBigData6-Aug-14 8:12 
QuestionVery good article Volodya! Pin
Volynsky Alex4-Feb-14 9:25
professionalVolynsky Alex4-Feb-14 9:25 
QuestionGood share Pin
Madhava Verma Dantuluri4-Feb-14 3:35
Madhava Verma Dantuluri4-Feb-14 3:35 

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

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