C# 当另一个切换为true时,如何将切换更改为false

C# 当另一个切换为true时,如何将切换更改为false,c#,wpf,xaml,C#,Wpf,Xaml,当我尝试关闭一个开关和打开另一个开关的逻辑时,我得到了堆栈溢出 我有两个开关。它们都侦听VM中的两个不同属性(IsAddAppleEnabled和isaddorangenabled) 我希望这样,当IsAddAppledEnabled为true时,isAddRangeEnabled为false。。。反之亦然 我试着在设置器中进行设置,但我最终进入了一个循环,并得到了一个stackoverflow异常 private bool isAddAppledEnabled= false;

当我尝试关闭一个开关和打开另一个开关的逻辑时,我得到了堆栈溢出

我有两个开关。它们都侦听VM中的两个不同属性(
IsAddAppleEnabled
isaddorangenabled

我希望这样,当
IsAddAppledEnabled
为true时,
isAddRangeEnabled
为false。。。反之亦然

我试着在设置器中进行设置,但我最终进入了一个循环,并得到了一个stackoverflow异常

    private bool isAddAppledEnabled= false;
    public bool isAddAppledEnabled
    {
        get { return isAddReplicateToggleChecked; }
        set
        {
            isAddAppledEnabled= value;
            isAddOrangeEnabled= !value;
            OnPropertyChanged(nameof(IsAddAppledEnabled));
        }
    }

    private bool isAddOrangeEnabled= false;
    public bool IsAddOrangeEnabled
    {
        get { return isAddOrangeEnabled; }
        set
        {
            isAddOrangeEnabled= value;
            isAddAppledEnabled= !value;
            OnPropertyChanged(nameof(IsAddOrangeEnabled));
        }
    }

您可以在属性集IsAddAppleEnabled中创建无限递归, 属性的名称是
isAddAppleEnabled
,您可以在集合中递归调用它

 isAddAppledEnabled= value;
改为

IsAddAppledEnabled = value

设置支持字段并引发两个属性的
PropertyChanged
事件。您还从
isAddAppleEnabled
属性返回了错误的值。试试这个:

private bool _isAddAppledEnabled;
public bool IsAddAppledEnabled
{
    get { return _isAddAppledEnabled; }
    set
    {
        _isAddAppledEnabled= value;
        _isAddOrangeEnabled= !value;
        OnPropertyChanged(nameof(IsAddAppledEnabled));
    OnPropertyChanged(nameof(IsAddOrangeEnabled));
    }
}

private bool _isAddOrangeEnabled;
public bool IsAddOrangeEnabled
{
    get { return _isAddOrangeEnabled; }
    set
    {
        _isAddOrangeEnabled= value;
        _isAddAppledEnabled= !value;
        OnPropertyChanged(nameof(IsAddOrangeEnabled));
    OnPropertyChanged(nameof(IsAddAppledEnabled));
    }
}
方案A(最小进近变化)

private bool _isAddAppledEnabled = false;
public bool IsAddAppledEnabled
{
  get { return _isAddAppledEnabled; }
  set
  {
    if (_isAddAppledEnabled == value)
      return;

    _isAddAppledEnabled = value;
    IsAddOrangeEnabled = !value;
    OnPropertyChanged(nameof(IsAddAppledEnabled));
  }
}

private bool _isAddOrangeEnabled = false;
public bool IsAddOrangeEnabled
{
  get { return _isAddOrangeEnabled; }
  set
  {
    if (_isAddOrangeEnabled == value)
      return;

    _isAddOrangeEnabled = value;
    IsAddAppledEnabled = !value;
    OnPropertyChanged(nameof(IsAddOrangeEnabled));
  }
}
选项B(布尔反转转换器): 您可以在视图模型中使用单个属性并利用转换器,以便xaml可以根据状态做出不同的反应

一个例子是简单地使用bool(像您一样)并应用反转转换器:

用法:

  <Button IsEnabled="{Binding IsAddAppledEnabled}">
  <Button IsEnabled="{Binding IsAddAppledEnabled, Converter={yourNamespace:InvertedBooleanConverter}}">
视图模型:

public enum Fruits { Apple, Orange, Pear, Grape }

private Fruits _selectedFruit;
public Fruits SelectedFruit
{
  get { return _selectedFruit; }
  set
  {
    _selectedFruit = value;
    OnPropertyChanged(nameof(SelectedFruit));
  }
}
Xaml用法:

<RadioButton Content="Apple" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Apple}}"/>
<RadioButton Content="Orange" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Orange}}"/>
<RadioButton Content="Pear" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Pear}}"/>
<RadioButton Content="Grape" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Grape}}"/>


设置“IsAddAppleEnabled”设置“IsAddAppleEnabled=value;”-这会导致无限递归-因此堆栈溢出。您是否打算将该属性命名为“IsAddAppledEnabled”,类似于“IsAddOrangeEnabled”,并带有一个专用支持字段。是的,很抱歉进行了一个小的更改,复制和粘贴错误。请注意,如果一个属性始终为true,则您可能只需要一个专用布尔字段&false。on的get属性返回私有布尔字段的值,另一个返回未指定的值。与set类似。PaulF您能举个例子吗?只需使用两个单选按钮。如果必须,请将它们模板化为togglebuttons。请注意我上面的评论-如果这两个属性始终彼此相反-则不需要isAddOrangeEnabled。IsAddOrangeEnabled的getter可以返回!IsAddAppleEnabled。@PaulF:是的,但是您不能单独设置
IsAddRangeEnabled
。是的,您可以-它已经将IsAddAppleEnabled设置为!值-只需删除setter中的行设置_isAddOrangeEnabled。@PaulF:为了澄清,如果不设置其他属性,则不能设置
isAddOrangeEnabled
。我猜OP有一个控件绑定到
IsAddOrangeEnabled
“public bool IsAddOrangeEnabled{get{return!isaddalappledabled;}set{isaddalappledabled=!value;}”消除了在WPF中使用_isaddorangeenabledconverter的需要
public enum Fruits { Apple, Orange, Pear, Grape }

private Fruits _selectedFruit;
public Fruits SelectedFruit
{
  get { return _selectedFruit; }
  set
  {
    _selectedFruit = value;
    OnPropertyChanged(nameof(SelectedFruit));
  }
}
<RadioButton Content="Apple" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Apple}}"/>
<RadioButton Content="Orange" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Orange}}"/>
<RadioButton Content="Pear" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Pear}}"/>
<RadioButton Content="Grape" IsChecked="{Binding Path=SelectedFruit, Mode=TwoWay, Converter={yourNamespace:EnumToBooleanConverter}, ConverterParameter={x:Static yourNamespace:Fruits.Grape}}"/>