WPF RadioButtons and data binding

There is a well-known issue with WPF RadioButton controls with data binding: when a radio button is unchecked the data binding is not undone. For example, suppose you have the following two radio buttons in the same group linked to a the "IsSuccess" (of type bool?) attribute of an object:

When you check the rbSuccess radio button, the IsSuccess field becomes "true", but when you check on the rbFailure radio button (and thus uncheck the rbSuccess one), the IsSuccess field is still true!

The problem is discussed here. There are several solutions. This blog explains one (using a control template). Another popular one is to use a ListBox restyled as a radio button. However, this causes other problems such as being unable to scroll past the radiobuttons (they are really a listbox, so scrolling is done inside the list).

The solution I have found is to put each radio button in a different group and link their checked/unchecked values via the converter. Like this:

And here is the converter:

Notice the converter parameter. Its role is to make sure that I get opposite values for the two radio buttons (when one is true, the other is false).

value
(IsSuccess) param

result
RadioButton.IsChecked = !(value ^ param)

true true true false true false true false false false false true

(^ is the XOR operator in C#)

This article is part of the GWB Archives. Original Author: Clara Oscura