简体   繁体   English

如何用枚举属性触发更改的属性?

[英]How to fire Property Changed with an Enum Property?

I have a standard Enum that will either be Yes or No: 我有一个标准的Enum,可能为Yes或No:

public enum YesOrNo
{
   Yes,
   No
}

My base Model Class has a YesOrNo property like this: 我的基本Model类具有YesOrNo属性,如下所示:

public class Group : NotifyPropertyChanged
{
  private YesOrNo groupOperator;
  public YesOrNo GroupOperator
  {
    get
    {
      return this.groupOperator;
    }
    set
    {
      this.groupOperator = value;
      OnPropertyChanged("GroupOperator");
    }
  }

In my View, I am using a ToggleSwitch, similar to a slider you would see on a Mobile phone. 在我的视图中,我使用的是ToggleSwitch,类似于您在手机上看到的滑块。 Sliding back and forth should effectively reassign the value of the Enum. 来回滑动应有效地重新分配Enum的值。 So it will default as Yes and sliding the toggle will set the Enum value to No and alternatively. 因此,它将默认设置为“是”,并且滑动切换开关会将“枚举”值设置为“否”,或者将其设置为。

If I were to have a test method that reassigns the Enum when the Checked command is hit, the PropertyChanged event is fired so I know that is technically working. 如果我有一种测试方法可以在按下Checked命令时重新分配Enum,则将触发PropertyChanged事件,因此我知道这在技术上是可行的。 I am just wondering how I could go about alternating values in the Enum. 我只是想知道如何在Enum中交替使用值。

This is the ToggleButton in my XAML: 这是我的XAML中的ToggleButton:

<ToggleButton Style="{StaticResource ToggleViewSwitch}" Command="{Binding SetOperatorCommand, UpdateSourceTrigger=PropertyChanged}" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"/>

And this is my Main View Model, where I hold the Command and the test method to assign the value manually: 这是我的主视图模型,在其中保存命令和测试方法以手动分配值:

  private bool isChecked = false;
    public bool IsChecked
    {
        get
        {
            return this.isChecked;
        }
        set
        {
            this.isChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }

 private RelayCommand setOperatorCommand;
    public ICommand SetOperatorCommand
    {
        get
        {
            if (this.setOperatorCommand == null)
            {
                this.setOperatorCommand = new RelayCommand(
                    x => ToggleGroupOperator());
            }
            return this.setOperatorCommand;
        }
    }

    private void ToggleGroupOperator()
    {
        if (IsChecked)
        {
            TopLevelGroup.GroupOperator = YesNo.No;
        }
        else
        {
            TopLevelGroup.GroupOperator = YesNo.Yes;
        }
    }

First make a Converter... 首先制作一个转换器...

public class YesOrNoToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => (value is YesOrNo yesOrNo && yesOrNo == YesOrNo.Yes) ? true : false;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => (value is bool isYes && isYes) ? YesOrNo.Yes : YesOrNo.No;
}

Then reference the converter during binding... 然后在绑定期间引用转换器...

<Window.Resources>
    <Converters:YesOrNoToBooleanConverter x:Key="YesOrNoToBooleanConverter" />
</Window.Resources>

<Grid>
    <CheckBox IsChecked="{Binding GroupOperator, Converter={StaticResource YesOrNoToBooleanConverter}}" />
</Grid>

This will allow your ViewModel to remain using the enum without any overhead and the view to bind without any overhead; 这将使您的ViewModel保持使用枚举而没有任何开销,并且视图绑定也没有任何开销; leave this binding manipulation work to converters. 将此绑定操作工作留给转换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM