user2143645
user2143645

Reputation: 45

WPF Radiobutton property binding

I am writing a wizard based application where, I load different usercontrol for each step. The usercontrol that is loaded has a radio button bound to 2 properties. when I try to load already existing usercontrol, the radiobutton status is not restored. The value of the property to which radio button is bound is set to false.

Below is the view and the model code snippet

public bool Yes
    {
        get
        {
            return _yes;
        }
        set
        {
            _yes = value; // Value is set to false whenever the view is reloaded.
            NotifyPropertyChanged(value.ToString());

        }
    }
    public bool No
    {
        get
        {
            return _no;
        }
        set
        {
            _no = value;

            Yes = !value;
            //if (value)
            //{
            //  Yes = !_no;
            //}
        }
    }

View:

<RadioButton  x:Name="Yes" GroupName ="Check" Content="Yes" Margin="24,0,0,0"  IsChecked="{Binding Yes, Mode=TwoWay}"/>
                <RadioButton  x:Name="No"   GroupName ="Check" Content="No" Margin="24,0,0,0" IsChecked="{Binding No,  Mode=TwoWay}"/>

Would like to know why and how the value gets set to false?

Upvotes: 0

Views: 94

Answers (2)

Babbillumpa
Babbillumpa

Reputation: 1864

You could use a single property and a converter that you can use as inverter:

private bool _IsYes;
public bool IsYes
{
    get
    {
        return _IsYes;
    }
    set
    {
        _IsYes = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsYes"));
    }
}

Here the BooleanInverter:

public class BooleanInverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return false;

        return !value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return false;

        return !value;
    }
}

The XAML could look like this:

Add the inverter as resource:

<Window.Resources>
    <local:BooleanInverter x:Key="Inverter"/>
</Window.Resources>

And use it:

<RadioButton Content="Yes" IsChecked="{Binding IsYes}"/>
<RadioButton Content="No" IsChecked="{Binding IsYes, Converter={StaticResource Inverter}}"/>

Upvotes: 0

mm8
mm8

Reputation: 169200

Where are you "restoring" the values? It should work if you set either the Yes or No property to true initially. Please refer to the following sample code where "Yes" is selected initially:

private bool _yes = true;
public bool Yes
{
    get
    {
        return _yes;
    }
    set
    {
        _yes = value; // Value is set to false whenever the view is reloaded.
        _no = !value;
        NotifyPropertyChanged("Yes");

    }
}

private bool _no;
public bool No
{
    get
    {
        return _no;
    }
    set
    {
        _no = value;
        _yes = !value;
        NotifyPropertyChanged("No");
    }
}

Upvotes: 1

Related Questions