简体   繁体   English

枚举更改事件

[英]Enum Changed Event

I am writing a WPF application and I would like to use an Enum for a State variable. 我正在编写WPF应用程序,并且想对状态变量使用枚举。

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. 如果您使用的是MVVM方法,那么我同意Daniel White的观点,即您需要实现INotifyPropertyChanged。 You should bind the IsEnabled member on your controls to a property on your ViewModel. 您应该将控件上的IsEnabled成员绑定到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: XAML:

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

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

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