简体   繁体   中英

How to fire Property Changed with an Enum Property?

I have a standard Enum that will either be Yes or No:

public enum YesOrNo
{
   Yes,
   No
}

My base Model Class has a YesOrNo property like this:

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. Sliding back and forth should effectively reassign the value of the 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. I am just wondering how I could go about alternating values in the Enum.

This is the ToggleButton in my XAML:

<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; leave this binding manipulation work to converters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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