Question

I have a property for userControl like this:

    public enum Mode { Full, Simple }
    public Mode NavigatorMode { get; set; }

and now, I need to write an event, when user change the property (NavigatorMode) form Full mode to simple mode, or reverse

how can I do that?

Was it helpful?

Solution

Implement INotifyPropertyChanged to your class:

public class YourClass : INotifyPropertyChanged
{
    // Your private variable
    private Mode mode;

    // Declare the event 
    public event PropertyChangedEventHandler PropertyChanged;

    public YourClass()
    {
    }

    public Mode NavigatorMode 
    {
       get { return mode; }
       set
       {
          mode = value;

          // Call OnPropertyChanged whenever the property is updated
          OnPropertyChanged(mode);
       }
    }

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(Mode modeParam)
    {
       PropertyChangedEventHandler handler = PropertyChanged;
       if (handler != null)
       {
          handler(this, new PropertyChangedEventArgs(modeParam));
       }
    }
 }

OTHER TIPS

How about implementing the INotifyPropertyChanged interface in your control?

Or simply writing a custom event:

public event EventHandler<Mode> ModeChanged;

public Mode NavigatorMode
{
    get { return _navigatorMode; }
    set 
    {
        _navigatorMode = value;
        if(ModeChanged != null)
            ModeChanged(this, _navigatorMode);
    }
}

And outside your usercontrol you can handle that event and do something based on the mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top