简体   繁体   中英

INotifyPropertyChanged with an Enum

still new to C# and WPF and well I want to use an Enum but I can't figure out how to use it with OnPropertyChanged

public enum _status
{
    AuthRequired, AuthAttempted, AuthReceived, AuthError, AuthSuccessful
}

this doesn't work:

public enum AuthStatus
{
  get { return _status; }
  set { ..... }
}

I know the solution is going to be very simple but i haven't found anything when I looked on google.. any help?

This simply will not work.

Part of the problem is that an enum , by definition, cannot set values, and for the normal use of enums you would never want to. This is simply how enums work.

Now, if your property in your model or viewmodel is an enum type, you can easily declare it as a property and raise property changes as with any other type.

private Status _status
public enum Status
{
    AuthRequired, AuthAttempted, AuthReceived, AuthError, AuthSuccessful
}

public Status Status
{
    get { return _status; }
    set
    {
        _status = value;
        RaisePropertyChanged("Status");
    }
}

You can't declare an enum as a property. Your code needs to be:

private _status _myStatus;
public _status AuthStatus
{
  get { return _myStatus; }
  set 
  { 
     _myStatus = value;
     NotifyPropertyChanged("AuthStatus")
  }
}

Writing public enum _status declares a new type called _status (note this isn't a very good name for a type, since it looks like a private data member). You then need to declare a property and field of this type that you can then run NotifyPropertyChanged on.

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