简体   繁体   中英

Enum Changed Event

I am writing a WPF application and I would like to use an Enum for a State variable.

Example: When the program start up certain controls are disabled until the state changes.

When the state changes I would like to disable/enable a variety of controls via an event handler. I have written plenty of custom event handlers in the past, however, using an enum as the trigger has managed to blow my mind.

Any suggestions?

您应该在视图模型中实现INotifyPropertyChanged并在值更改时调用事件。

If you're using an MVVM approach then I agree with Daniel White that you need to implement INotifyPropertyChanged. You should bind the IsEnabled member on your controls to a property on your ViewModel.

Code:

public class ViewModel : INotifyPropertyChanged
{
      public MyEnum EnumValue 
      { 
           get { return enumValue; } 
           set { 
                 enumValue = value;
                 AreControlsEnabled = enumValue == MyEnum.SomeValue;
           }
      }

      public bool AreControlsEnabled 
      {
           get { return areControlsEnabled; }
           set {
                 areControlsEnabled = value;
                 if (PropertyChanged != null)
                     PropertyChanged(this, new PropertyChangedEventArg("AreControlsEnabled");
            }
      }

      public event PropertyChangedEventHandler PropertyChanged;
}

XAML:

<TextBox IsEnabled="{Binding AreControlsEnabled}"/>

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