Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF Radiobutton属性绑定_Wpf_Mvvm_Data Binding - Fatal编程技术网

WPF Radiobutton属性绑定

WPF Radiobutton属性绑定,wpf,mvvm,data-binding,Wpf,Mvvm,Data Binding,我正在编写一个基于向导的应用程序,其中,我为每个步骤加载不同的usercontrol。加载的usercontrol有一个绑定到2个属性的单选按钮。当我尝试加载已经存在的usercontrol时,radiobutton状态不会恢复。单选按钮绑定到的属性的值设置为false 下面是视图和模型代码片段 public bool Yes { get { return _yes; } set {

我正在编写一个基于向导的应用程序,其中,我为每个步骤加载不同的usercontrol。加载的usercontrol有一个绑定到2个属性的单选按钮。当我尝试加载已经存在的usercontrol时,radiobutton状态不会恢复。单选按钮绑定到的属性的值设置为false

下面是视图和模型代码片段

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;
            //}
        }
    }
视图:


想知道为什么以及如何将值设置为false吗?

在哪里恢复值?如果您最初将Yes或No属性设置为true,那么它应该可以工作。请参考以下示例代码,其中最初选择的是:

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");
    }
}

您可以使用单个属性和转换器作为逆变器:

private bool _IsYes;
public bool IsYes
{
    get
    {
        return _IsYes;
    }
    set
    {
        _IsYes = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsYes"));
    }
}
这里是布尔反相器:

XAML可以如下所示:

将逆变器添加为资源:

<Window.Resources>
    <local:BooleanInverter x:Key="Inverter"/>
</Window.Resources>
并使用它:

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

这可能是因为双向绑定,或者是代码的另一部分。传递value.ToString也是错误的!!!!一个布尔状态有两个依赖属性的意义何在,是或否?
<Window.Resources>
    <local:BooleanInverter x:Key="Inverter"/>
</Window.Resources>
<RadioButton Content="Yes" IsChecked="{Binding IsYes}"/>
<RadioButton Content="No" IsChecked="{Binding IsYes, Converter={StaticResource Inverter}}"/>