Click here to Skip to main content
14,392,303 members

Generic ValueConverter for Binding Enumerations to Check Boxes

Rate this:
4.75 (13 votes)
Please Sign up or sign in to vote.
4.75 (13 votes)
15 Oct 2011CPOL
Generic ValueConverter for Binding Enumerations to Check Boxes


There are many times that there are a group of radio buttons on a form that are associated with a single enumeration. What many developers will do is create a separate property in the ViewModel for each radio button. I do not like this. First I think that this is too much of a dependency between the View and the ViewModel. It also adds a whole bunch of properties to the ViewModel that just makes the ViewModel bigger, and it requires excess code for each property to convert it to and from an enumeration. All that is really needed is a single property for each enumeration, and a generic ValueConverter.


After so many times of having to deal with radio buttons and ViewModels, I finally decided to create a generic way of dealing with them.


To bind an enumeration to the IsChecked property of a group of radio buttons, a ValueConverter will be required; there is just no way to bind an enumeration to a bool without converting it. It is possible to create a separate ValueConverter class for each radio button, but that is not required since the ValueConverter methods have a parameter argument which is set in the XAML by using the ConverterParameter argument of the Binding. The idea is that each radio button IsChecked argument is bound to the Enumeration property using the EnumValueConverter with a ConverterParameter equal to a string which is the name of the specific enumeration.

class EnumValueConverter : IValueConverter
	public object Convert(object value, Type targetType, 
		object parameter, System.Globalization.CultureInfo culture)
		if (value == null || parameter == null)
			return false;
		return value.ToString() == parameter.ToString();

	public object ConvertBack(object value, Type targetType, 
		object parameter, System.Globalization.CultureInfo culture)
		if (value == null || parameter == null)
			return null;
		var rtnValue = parameter.ToString();
			object returnEnum = Enum.Parse(targetType, rtnValue);
			return returnEnum;
			return null;

In the example code, I have added three debug methods to aid in finding errors, but have removed these methods for clarity. As can be seen, the code is fairly straight forward. In the Convert method, the ToString() method is used to convert the enumeration from the ViewModel to a string which is then compared to the ConverterParmeter; if equal, a the value returned is true. In the ConvertBack method, the Enum.Parse method is used to convert the ConverterParmeter from the control into the appropriate enumeration for the ViewModel if the value argument in. This fortunately works because in the case of radio buttons, they only need to deal with being changed to the checked state, so never have to deal with changes to the unchecked state; that is automatic. If the Parse does not work, a null is returned (which is also the case for null value and parameter argument, which has no effect since null is invalid for an enumeration (Binding does not allow changes for invalid values).

Wiring up the XAML to the property and converter is quite straightforward. In the example, there is a very simple enum:

public enum TestEnum
	A, B, C

The property in the ViewModel for this enumeration is also very straightforward:

public TestEnum EnumBinding
		return _enumBinding;
		if (_enumBinding != value)
			_enumBinding = value;
			if (PropertyChanged != null)
				PropertyChanged(this, new 

Now all that is needed is the XAML, which includes the resource definition for the converter and the checkboxes with the binding definition using the converter and the ConverterParameter:

<Window x:Class="GenericEnumValueConverter.MainWindow"




		Title="Enumerator Value Converter Example" 

		Height="200" Width="250">
		<local:EnumValueConverter x:Key="EnumValueConverter"/>
	<StackPanel HorizontalAlignment="Center" 

		<RadioButton Content="A" 

			IsChecked="{Binding EnumBinding,
				Converter={StaticResource EnumValueConverter},
		<RadioButton Content="B" 

			IsChecked="{Binding EnumBinding,
				Converter={StaticResource EnumValueConverter},
		<RadioButton Content="C" 

			IsChecked="{Binding EnumBinding,
				Converter={StaticResource EnumValueConverter},

The actual example includes a TextBox to confirm that the property for the enumeration is actually the value of the radio buttons, and an extra radio button containing a bad value to show what happens when there is a bad enumeration value in the XAML (during debug, pressing the “Bad Value” radio button creates a message in the Output window).


This is a very clean way to connect an enumeration to radio buttons. The same converter can also be used for other purposes with a little bit of extra code, such as setting the visibility of controls based on a state that is specified by an enumeration (need to covert the bool value to a Visibility enumeration). It removes a lot of complexity from the ViewModel, and uses a single converter for all instances where enumerations are bound to a CheckBox (or another Control). In all, it is a very clean way to reduce code, and improve maintainability.


  • 15th October, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Clifford Nelson
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at

Comments and Discussions

GeneralMy vote of 1 Pin
DontCareAboutCode3-Nov-12 2:52
MemberDontCareAboutCode3-Nov-12 2:52 
GeneralRe: My vote of 1 Pin
Clifford Nelson3-Nov-12 7:53
mvaClifford Nelson3-Nov-12 7:53 
GeneralRe: My vote of 1 Pin
DontCareAboutCode3-Nov-12 8:01
MemberDontCareAboutCode3-Nov-12 8:01 
Dear Clifford Nelson,

I was just indignant about the fact you give a vote of 3 just because you think it should have been a 'Tip'. If you posted a normal comment without a vote, I would have changed my 'Article' to a 'Tip'. I must admit that I didn't even knew about the fact that Tip's existed. Maybe they didn't at the time I posted my article, don't know for sure. But hey, I removed the 'Article' since I really don't care about others using the code or not, just wanted to share, it had 1800 downloads so I hope those ppl can use it.

Kind regards,
QuestionThoughts Pin
PIEBALDconsult15-Oct-11 6:13
professionalPIEBALDconsult15-Oct-11 6:13 
AnswerRe: Thoughts Pin
Clifford Nelson15-Oct-11 6:47
mvaClifford Nelson15-Oct-11 6:47 
AnswerRe: Thoughts Pin
Sacha Barber16-Oct-11 22:51
mvaSacha Barber16-Oct-11 22:51 
GeneralRe: Thoughts Pin
PIEBALDconsult17-Oct-11 3:53
professionalPIEBALDconsult17-Oct-11 3:53 
GeneralRe: Thoughts Pin
Jürgen Röhr24-Oct-11 4:14
professionalJürgen Röhr24-Oct-11 4:14 

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.

Posted 15 Oct 2011

Tagged as


17 bookmarked