简体   繁体   中英

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

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.

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.

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.

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