.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > RadioButton IsChecked Binding not sending uncheck event
Ask a questionAsk a question
 

QuestionRadioButton IsChecked Binding not sending uncheck event

  • Monday, March 26, 2007 8:53 PMBernd Helzer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I am trying to databind several radiobuttons to some objects that have a checked property. If a click on an unselected radio button, my databound object is updated. However the other object that should now be not checked is not. The code is fairly simple.

    Am I missing something here or is this a bug in databinding for RadioButtons?

    <Window x:Class="RadioTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Radio Button Test" Height="154" Width="285">

    <StackPanel Margin="5">

    <RadioButton Name="A" IsChecked="{Binding Path=Checked}" GroupName="Group">Yes</RadioButton>

    <RadioButton Name="B" IsChecked="{Binding Path=Checked}" GroupName="Group">No</RadioButton>

    <CheckBox Name="C" IsChecked="{Binding Path=Checked}">Yes</CheckBox>

    <CheckBox Name="D" IsChecked="{Binding Path=Checked}">No</CheckBox>

    </StackPanel>

    </Window>

    public partial class RadioTest : System.Windows.Window

    {

    MyData _Yes = new MyData();

    MyData _No = new MyData();

    public RadioTest()

    {

    InitializeComponent();

    _Yes.Checked = true;

    A.DataContext = _Yes;

    C.DataContext = _Yes;

    B.DataContext = _No;

    D.DataContext = _No;

    }

    }

    public class MyData : INotifyPropertyChanged

    {

    private bool _Checked;

    public bool Checked

    {

    get { return _Checked; }

    set

    {

    _Checked = value;

    OnPropertyChanged("Checked");

    }

    }

     

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)

    {

    if (PropertyChanged != null)

    {

    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

    }

    #endregion

    }

    }

All Replies

  • Monday, March 26, 2007 9:21 PMDrew MarshModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    You need to set Mode=TwoWay on your Bindings. The default is typically OneWay (unless a DP specifies BindsTwoWayByDefault) which means changes to the elements won't propagate changes back to their datasource.

     

    HTH,
    Drew

  • Monday, March 26, 2007 10:17 PMBernd Helzer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    A simple solution like that would have been nice. Luckilly, the WPF designers decided that the binding for IsChecked should be TwoWay by default. Otherwise I wouldn't get the update for Checked either. So setting the Binding to TwoWay does not change the behaviour in this case.

    As it stands I get a Checked update, but no Unchecked on RadioButtons. On the CheckBox however I get both Checked and Unchecked updates.

     

  • Thursday, March 29, 2007 9:37 PMapf Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm seeing the same problem. I have a value converter for the binding. The ConvertBack method of my value converter is only getting called once per radio button group. I.e., there are 2 radio buttons. Initially, neither one is set. I click on the first one and ConvertBack is not called. I click on the second one and ConvertBack is called for that one. After that, clicking on either one does not cause the ConvertBack method to be called.

     

    Sounds like a bug in RadioButton binding. I guess I'll go back to my event handlers.

  • Tuesday, April 03, 2007 7:20 PMapf Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    How can I file a bug on this issue? It is very badly broken. I removed my value converter and am now binding the radio button directly to a database field. When the button is checked, the value for the checked button is updated but the value for the unchecked button is not. No problem, I thought, I'll just add an Unchecked event handler and force the source update. I added the following code:

     

    private void radioButton_Unchecked(object sender, RoutedEventArgs e)

    {

    // Force update of the source when a radio button is unchecked. The source is updated

    // correctly when a radio button is checked but not when it is unchecked.

    BindingExpression be = ((RadioButton)sender).GetBindingExpression(RadioButton.IsCheckedProperty);

    be.UpdateSource();

    }

     

    When this method is called on initial startup, when the value of the bound fields are initialized to false, it works fine. However, when it is called when another radio button in the group is checked, the value of the BindingExpression is null! No wonder the source is not updated.

     

    Here is the XAML for reference:

     

    <RadioButton Name="dayOfMonthRadioButton" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right"

    Margin="5,10,10,0" Content="Day"

    IsChecked="{Binding hasdayofmonth, Mode=TwoWay}"

    Checked="dayOfMonthRadioButton_Checked" Unchecked="radioButton_Unchecked">

     

    The data context is set in the grid declaration.

     

    Is there something I'm missing, or is this just a bug in radio button binding?

  • Thursday, April 19, 2007 4:28 PMBernd Helzer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Sorry, I have been offline for a while. Do we have a solution for this problem?

  • Thursday, April 19, 2007 7:00 PMTamir Khason Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This is not actually bug. While trying to do something, that has application logic (e.g. explicitly set value or uncheck the checkbox by group logic), you are lost your binding. That's the explanation for this behavior

    You can change you XAML following to see this in action

     

    Code Snippet

    <RadioButton Name="A" IsChecked="{Binding Path=Checked}" GroupName="Group1">Yes</RadioButton>

    <RadioButton Name="B" IsChecked="{Binding Path=Checked}" GroupName="Group2">No</RadioButton>

    <CheckBox Name="C" IsChecked="{Binding Path=Checked}">Yes</CheckBox>

    <CheckBox Name="D" IsChecked="{Binding Path=Checked}">No</CheckBox>

     

  • Friday, April 20, 2007 11:34 PMBernd Helzer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have to disagree with that statement.

    What value is there in databinding if it doesn't automatically update when the button gets unchecked?

     

    A data bound CheckBox, for example, maintains it binding whether the value changes by clicking on it, or by changing the bound field. The only way to uncheck a RadioButton is to check another RadioButton in the same group. So unchecking a RadioButton should behave the same way as unchecking a CheckBox.

     

    Not calling it a bug in this case is just playing with words. Sure you can call it a limitation, saying you can only Databind to RadioButtons with only one Button per group, but in the end that is worthless, and the real feature is still not working.

  • Saturday, July 14, 2007 2:47 AMKim Johnson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm seeing the same problem with the BindingExpression going to null after certain actions.  Here's the XAML for the radio buttons:

    Code Snippet

    <StackPanel>
           <RadioButton Name="mRadioButton1" GroupName="Country"
            IsChecked="{Binding Path=Country, Mode=TwoWay, Converter={StaticResource rbConverter}, ConverterParameter=UK}"Content="UK" />
          <RadioButton Name="mRadioButton2" GroupName="Country"
            IsChecked="{Binding Path=Country, Mode=TwoWay, Converter={StaticResource rbConverter}, ConverterParameter=USA}" Content="USA" />
     </StackPanel>

     

    The DataContext is set for the window to a CollectionView containing a list of Employee objects.  The "rbConverter" is a simple value converter.  There are no event handlers in place.

     

    Initially when the window opens both radio buttons are bound correctly.  The bindings for one or both radio buttons will eventually be "lost" though, but I can't quite pin down the use cases.  In one case,  a binding is lost while scrolling through the Employees (the CurrentItem changes) as the bound value changes with each employee - eg, Emp1 has "USA", Emp2 has "UK", Emp3 has "USA" and bang, the binding is lost for the second button.  Second use case involves clicking a radiobutton to change the bound value and then scrolling through the list - as the value changes back and forth and back again the binding is lost.  With a little luck, both buttons become unbound Smile.

     

    I have to think that this is a bug with radio button binding.  Any hope of having this acknowledged as a bug and fixed?

  • Monday, September 10, 2007 10:10 PMpkellnerMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Has there been any progress or good workaround on how to work with radiobuttons in lists? (not sure how a single radio button will ever do me any good).  I just spend many hours until I ran into this thread.

  • Thursday, May 01, 2008 4:29 PMIsotope235 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm not sure if this thread is still alive or not, but this question has been answered in the following thread: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2052600&SiteID=1

     

    In short, to accomplish the default behavior of RadioButtons, the RadioButton class has built-in handling of its Checked event to locate all its RadioButton siblings (with the same groupname, if applicable) and uncheck them.  Similar to setting otherRadioButton.IsChecked manually, this method of explicitly setting the value clears the binding associated with the IsChecked property.