Geeks With Blogs
Clara's Notes on Technology Notes to me and the world

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:

<RadioButton Grid.Row="0" Grid.Column="0" GroupName="rbGroup" Margin="8,0,0,0" VerticalAlignment="Center" Content="Success" x:Name="rbSuccess" IsChecked="{Binding Path=IsSuccess, Mode=TwoWay, Converter={StaticResource nullableBooleanConverter}}" /> <RadioButton Grid.Row="0" Grid.Column="1" GroupName="rbGroup" Margin="8,0,0,0" VerticalAlignment="Center" Content="Failure" x:Name="rbFailure" />

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:

<RadioButton Grid.Row="1" Grid.Column="0" GroupName="rbGroupSuccess" Margin="8,0,0,0" VerticalAlignment="Center" Content="Success" x:Name="rbSuccess" IsChecked="{Binding Path=IsSuccess, Mode=TwoWay, Converter={StaticResource nullableBooleanConverter}, ConverterParameter=true}" /> <RadioButton Grid.Row="1" Grid.Column="1" GroupName="rbGroupFailure" Margin="8,0,0,0" VerticalAlignment="Center" Content="Failure" x:Name="rbFailure" IsChecked="{Binding Path=IsSuccess, Mode=TwoWay, Converter={StaticResource nullableBooleanConverter}, ConverterParameter=false}" />

And here is the converter:

[ValueConversion(typeof(bool?), typeof(bool))] public class SuccessConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { bool param = bool.Parse(parameter.ToString()); if (value == null) { return false; } else { return !((bool)value ^ param); } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { bool param = bool.Parse(parameter.ToString()); return !((bool)value ^ param); } }

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

Technorati Tags: ,

 

Posted on Friday, October 17, 2008 8:46 AM WPF | Back to top


Comments on this post: WPF RadioButtons and data binding

# re: WPF RadioButtons and data binding
Requesting Gravatar...
I'm sorry but I am definitely a newbie...What do you mean by "linked to a the "IsSuccess" attribute of an object"?
Left by Al on Dec 18, 2008 10:29 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Thx a lot. This really saved my day!
Left by Frederico Teles on Apr 17, 2009 11:31 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Hi. Thanks for the very straightforward tutorial.
I'm trying to use RadioButtons and binding them to a boolean property in the DataContext.
This is a TwoWay binding.
The RadioButton DOES display the present state of the DataContext Property.. but ANY changes to the button state on the UI do NOT trigger the Property Setter. This having been checked within the object value and by putting a breakpoint in the Setter of the property.
Would you have a clue as to what I may be doing wrong?

Thanks!
Left by Arvind on May 22, 2009 12:24 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Your solution does not work. It shows some compilation errors.
Left by Fayaz on Jun 02, 2009 11:45 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Thanks,

It really worked, However why should the group name be different?

Left by Raul Dsouza on Jul 13, 2009 5:13 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
So... what do you do if you have 3 or more radio buttons?
Left by William Garrison on Jul 15, 2009 9:54 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Thanks Clara,

Works excellent.
Left by Frank on Aug 14, 2009 3:35 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
I generally make an 'EqualityValueConverter' which does the same sort of thing but is generic for any type (and therefore can be used in other scenarios aswell)
Left by MF on Aug 18, 2009 2:22 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Wasn't working for me until I realized I hadn't set different group names. Now it works great thanks!
Left by jj on Sep 10, 2009 12:37 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
I must be really stupid at this programming thing, but isn't this:

!((bool)value ^ param)

Equal to this:

(bool)value == param

This ensures that the values are equivalent and it's a billion times easier to read!
Left by Nick on Sep 18, 2009 9:35 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
A not so elegant but failsafe and much simpler solution is to connect the Checked Event to the ViewModel by hand as so:

private void All_Checked(object sender, System.Windows.RoutedEventArgs e)
{
if (ViewModel != null)
{
ViewModel.ShowAll = true;
}
}

private void SelectedOnly_Checked(object sender, System.Windows.RoutedEventArgs e)
{
if (ViewModel != null)
{
ViewModel.ShowSelectedOnly = true;
}
}

This will do until WPF matures enough to not contain obvious bugs like these.
At that point I can remove the EventHandler and bind directly without changing much of my code.
Left by Thorsten on Oct 18, 2009 10:46 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
What if you're binding to an enum and there are more than two radio buttons? What would the converter look like then?
Left by Brian on Oct 22, 2009 11:08 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
It works. Thank you!
Left by Sam on Dec 03, 2009 3:11 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Hi, the solution doesn't work for me, it keeps both RadioButtons checked.
Left by Carlo Toribio on Mar 02, 2010 5:38 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
nvm, it suddenly worked, not sure why. Thanks
Left by Carlo Toribio on Mar 02, 2010 5:43 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
It's doesn't work for me.(.NET Framework 4)
Left by yuxs on Jun 03, 2010 5:36 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
If I have these radio buttons in a list box (which represents a child table) and are bound to a column in the child table...when I add another list item, these two radio buttons are presented again. Now if you select "Success" in the second row - if you have selected 'Success' for the first one, it will be un-checked... Is there something wrong in this?
Left by Raghu on Jul 01, 2010 6:45 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Most wired thing about RadioButton in wpf is, if you have two user control (say ctrl1 and ctrl2) sitting on same user control (say parentControl) and both the user controls (ctrl1 and ctrl2) have same GroupName of RadioButtons then all the RadioButton behave as if they are sitting on the same group though they are sitting in different user control.
Left by Humayun Kabir Foysol on Jul 27, 2010 7:58 AM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
I used this solution for several months in my MVVM project. Users reported random application crashes and I finally got one user to sit at my desk and work in a debug version of my application in VS2010. The IsSuccess property was causing a stack overflow exception. I'm not going to investigate why - I took it out and everything is working fine.
Left by MrMad on Apr 07, 2011 4:40 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
Would the converter be in the view model of a MVVM solution?
Left by Tanner Dozark on Feb 10, 2012 6:01 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
ConvertBack should be like:
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool param = bool.Parse(parameter.ToString());
return param;
}
Left by Paul Langemeijer on Oct 18, 2012 1:01 PM

# re: WPF RadioButtons and data binding
Requesting Gravatar...
take out the groupname, you don't need it anyway since the ischecked is set by a converter.

For some reason this really screwed things up when I had 2 instances of the control. It ran the converter twice when I only binded to 1 object.
Left by Dan on May 16, 2014 1:50 AM

Your comment:
 (will show your gravatar)
 


Copyright © Clara Oscura | Powered by: GeeksWithBlogs.net | Join free