1

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?

4

2 回答 2

3

You should implement INotifyPropertyChanged in your view model and invoke the event when the value changed.

于 2012-04-25T00:30:37.810 回答
2

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}"/>
于 2012-04-25T00:49:51.297 回答