How do I sign up for an event that's involved in linking INotifyPropertyChanged?



  • I need to go to ViewModel or MainWindows.xaml.cs when there's a change in the bool Edit connection. How can this be realized, i.e. how do we sign for the event of a change in the nature of the connection?

    In the programme, standard application of the link + is an event of the beginning and end of the editing for the text unit:

    IsInEditMode="{Binding Edit, Mode=TwoWay}"
    

    In the class used in Word.cs it is described as:

    public bool Edit
    {
        get
        {
            return _edit;
        }
        set
        {
            _edit = value;
            Notify("Edit");
        }
    }
    

    The method is in a separate class of Model Notify.cs:

    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propertyName)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    


  • INotifyPropertyChanged is not some magical system interface that can only use the dotnet itself and others cannot touch it. If you need a change notice, just sign for a change.

    var model = new ModelNotify();
    model.PropertyChanged += OnModelPropertyChanged;
    

    void OnModelPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
    if (e.PropertyName == nameof(ModelNotify.Edit))
    {
    // свойство Edit изменилось
    }
    }

    If you need a notice in the vimodel, you'll just change the grid. Or you can override the PropertyChanged if it's virtual, depending on your implementation INPC.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2