Home > database >  MPF MVVM object not fire property changed event when a field is changed
MPF MVVM object not fire property changed event when a field is changed

Time:06-16

I have object user

public class User
{
  public string FirstName {get; set:}
  public string LastName {get; set;}
}

My ViewModel implemented INotifyPropertyChanged

private User _myUser;
public User MyUser
{
    get
     {
       return _myUser;
     }
    set
     {
        _myUser = value;
        OnPropertyChanged("MyUser");
     }
}

Textbox object of xaml is binding to MyUser.FirstName, Mode = TwoWay

When the text field of the TextBox is changed set property of the object is not fired. What should be changed to fire a set property of the MyUser object.

CodePudding user response:

You're not setting the MyUser property so why would an event be raised telling you that you are? Your view model should have separate properties for the first name and last name if those are the values that will be changing.

CodePudding user response:

If you bind to MyUser.FirstName, it's only the setter of the FirstName property of User object that is returned by the MyUser property that will fire:

public class User
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; //<- set a breakpoint here }
    }

    public string LastName { get; set; }
}

The TextBox sets a string property. It cannot set a User property.

  • Related