Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi Guys, I have just (luckily) put together a simple working application with WPF MVVM. I understood (kind of) the gist of it, but there are still a few things that I still can't get around to

Suppose in:
C#
private string firstname;

public string FirstName
{
     get {return firstname;}
     set
       {
          if ( value != firstname)
           {
              firstname = value;
           }
           OnPropertyChanged("FirstName");
       }
}


So if I'm not mistaken, when we set a value into FirstName, the OnPropertyChanged() will update the bound property in the view and change its value.

But when I try it in the get{}, I get the warning code is unreachable. I had planned to pull out a value from get and then set off OnPropertyChanged(). What I currently did in my viewmodel is this:

C#
private Person p;

public string Id
{
    get {return p.Id;}
    set 
      {
         if (value != p.Id)
          {
             p.ID = value;
          }
          OnPropertyChanged("Id");
      }
}

public string FirstName
{
    get {return p.FirstName;}
}

protected void GetFirstName()
{
     Id = "PR001";
     OnPropertyChanged("FirstName");
}


Is there another way to do this or do I need to keep on calling OnPropertyChanged() whenever I want to update the property in the view?
Posted

1 solution

Not sure if I understand the question correctly, but you should call OnPropertyChanged only when the value of the property is actually changed and preferably inside the setter of a property and only for that property.

Based on your code you call OnPropertyChanged for the FirstName property even though it's value hasn't been changed. Only the value of Id property is changed in the GetFirstName method.
 
Share this answer
 
Comments
Lyandor 22-Aug-15 9:31am    
Hi Mika, thanks for answering. You got what I tried to ask. what you said matches my understanding for OnPropertyChanged().

At first I tried putting OnPropertyChanged("FirstName"); at the setter of Firstname's property, however, the (let's say) textblock did not update itself despite the value of p.FirstName changed after p.Id has been set via GetFirstName(). I tried what I wrote before out of curiosity and whenever GetFirstName() is triggered, the textblock updates. I don't understand why this happened.
Wendelius 22-Aug-15 12:25pm    
Not sure if I understand correctly, but is the ID also controlling other properties, meaning, if you change id also FirstName changes? If that is correct, add also

OnPropertyChanged("FirstName");

on Id setter. A single setter can notify all property changes in case the change is done via the backing field and the setter of another property isn't called.
Lyandor 22-Aug-15 13:58pm    
That makes perfect sense! I wonder why i have never thought of that before. Thanks a lot!
Wendelius 22-Aug-15 14:11pm    
Glad to be of service :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900