WPF radio buttons - MVVM - binding seems to die?
Asked Answered
E

3

7

I've bound the DataContext of the following Window to the code behind to give me a MVVM style to demonstrate this behaviour:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <StackPanel>
        <RadioButton GroupName="test" Content="Monkey" IsChecked="{Binding IsMonkey}"/>
        <RadioButton GroupName="test" Content="Turtle" IsChecked="{Binding IsTurtle}" />
    </StackPanel>
</Window>

Heres the code behind:

public partial class Window1
{
    public Window1()
    {
        InitializeComponent();
    }

    private bool _isMonkey;
    public bool IsMonkey
    {
        get { return _isMonkey; }
        set
        {
            _isMonkey = value;
        }
    }

    private bool _isTurtle;
    public bool IsTurtle
    {
        get { return _isTurtle; }
        set
        {
            _isTurtle = value;
        }
    }
}

Putting a breakpoint on the set of IsMonkey and IsTurtle and then running the application and selecting IsMonkey and IsTurtle after each other I found that it works for the first selection of each control, on the second selection the binding breaks and the breakpoints are no longer triggered?

Can anyone point me in the right direction, please?

Expound answered 20/9, 2010 at 9:53 Comment(0)
T
11

Your example has no Changed-notification. Your wrote it's only an example for an MVVM-style construction. Therefore I assume, you have implemented INotifyPropertyChanged or the properties are DependencyProperties. If not, the first thing you have to do is change-notification.

If you have change-notification, give the RadioButtons different group names (another name for each instance). This decouples them and the binding will not be broken anymore.

<StackPanel> 
    <RadioButton GroupName="test1" Content="Monkey" IsChecked="{Binding IsMonkey}"/> 
    <RadioButton GroupName="test2" Content="Turtle" IsChecked="{Binding IsTurtle}" /> 
</StackPanel> 

Depending on the declaration of your properties, it may also be meaningfull to declare the Binding TwoWay.

IsChecked="{Binding IsMonkey,Mode=TwoWay}

Hope this helped.

Torticollis answered 20/9, 2010 at 9:59 Comment(2)
Hi, thanks - got it working through different group names as you suggested and some code on the properties. Its annoying though, as it is the same group - so I wanted to use that functionality, but it wont let me.Expound
give the RadioButtons different group names My God, You saved my butt.Haight
F
2

This is a known issue with WPF radio buttons; check these posts for more details and workarounds -

WPF RadioButtons and data binding: http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx

Parsimony WPF Radio button binding (Enums and bug fixes): http://inquisitorjax.blogspot.com/2009/02/wpf-radio-button-binding-enums-and-bug.html

RadioButton unchecked bindings issue still not resolved? http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8eb8280a-19c4-4502-8260-f74633a9e2f2/

Finish answered 20/9, 2010 at 10:30 Comment(0)
P
0

This problem has been fixed in WPF 4.0, I'm leveraging this behavior in my own project right now. Just put the radio buttons into the same group, there's no longer any need to use different groupnames. The bindings will not be broken...in fact the "de-selected" radio button will have its bound value set to false as expected.

Plough answered 22/5, 2015 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.