WPF Radio Buttons… “Once it is checked it just won’t uncheck”

Today I was doing some Sunday morning coding (why read newspaper when you can code right? ) and stumbled upon an issue with Radio buttons in WPF… Basically if you have a set of Radio buttons with the same group name, once you check one of the radio buttons you cannot uncheck it.. This was causing me grief since I wanted to enable the user to be able to uncheck it back…

This might seem quite simple to solve but I wanted to write this post to show how WPF Dependency property system is superior from any other XAML platform …

So first off, I don’t want to create a sub class for the radio button to have this behavior and I would also like that i can re-use this behavior for other Radio buttons… The first thing that pops to mind is, “let’s create an attached property”. However with an attached property on it’s own we cannot do much… the idea of using an attached property works well if you can hook to an event and react accordingly adding the behavior that you want. However in this case what is happening is that the radio button is automatically suppressing the checked and unchecked events if a Radio button in a group is checked …

FrameworkMetaData to the rescue …

One thing that is not heard of much however it is there and a very important part of the WPF DependencyProperty System is CoerceValueCallback. With CoerceValueCallback you can validate the value of a property before it is set.. Example let’s say the Minimum and Maximum properties of a slider control, the Minimum property can have a CoerceValueCallback that validates that the Minimum value is never set more than the maximum..

In this case we can use the CoerceValueCallback to auto set IsChecked to false if the previous value of the Checkbox is set to true. The only issue now is that FrameworkMetaData get’s attached per Control Type, thus if I use the FrameworkMetaData it will be applied to all radio buttons in my solution… well for that we can use Attached properties… we can have an attached property and when set on a radio button we will enable this behavior if not then we don’t do anything …



and here is the attached property



And that’s it…

Silverlight, WinRT, WinPhone, they are really good XAML platforms, however for me WPF is just great for devs.. it gives the dev the power and flexibility needed to build clean and large scale solutions… Aaa well, maybe it’s passion from my first love Avalon speaking now… the good old day !

If you want the actual code so that you don’t need to re-code it, you can find it here 





8 thoughts on “WPF Radio Buttons… “Once it is checked it just won’t uncheck”

  1. Hhhmmmm… if you want to be able to uncheck, why not use a checkbox? Radio buttons are supposed to choose between things — and they uncheck when another option is chosen. Your users will be totally confused.

  2. @Stephen – What if you want zero or exactly one selections from a group? Checkbox won’t work there…
    The problem with radio buttons is that initially they might be un-checked. But if you check one by mistake also then you can’t uncheck them any way…This code might be useful in that case..

  3. Actually this is a very common scenario and a lot of people are RadioButton fanatics out there. They only say ‘thats NOT what RadioButton is meant for’.

    Thanks for the post, you saved me a lot of work!!!

  4. I think that’s a misuse of value coercion.

    Value coercion’s goal is to link a “wanted value” to a “meaningful value” in the target’s domain.

    In other words the “meaningful value” is a representation of the “wanted value” in the target’s domain.

    What you do here is not coercion. The property wants to be set to “Checked” (“wanted value”), but you forbid it and set it to “Unchecked” (“meaningfull value in the target’s domain”) instead. The “meanful value in the target’s domain” is no longer a representation of the “wanted value”, and that is wrong.

    Two problems related to this fundamental flaw : First, the binding on the “IsChecked” property is not triggered after the value coercion (and that’s normal because bindings target properties value and not their coerced values). Then, if for some reason you happen to set the IsChecked property of the RadioButton when it is already checked, it gets unchecked..

    Therefore, I think RadioButtons are just not built to be uncheckable, I might consider using an exclusive checkbox instead.

  5. Pingback: Unselect RadioButton using CoerceValueCallback and Attached Property | DEVeryday Life

  6. If WPF Radio Buttons (not “radiobutton” in general…) would be uncheckable, then you would not have the possibility to have no button checked in a group… Unfortunatly, it is not the case since the default state is any radio buttons checked.
    And users can check one by mistake.

    Another solution is to give a “clear” or “reset” function to this option group if you don’t want to make the radio buttons uncheckable and offers a better UX…

    Anyway, thanks Marlon for this solution!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s