I’m working on a WP7 app and I’ve had some trouble updating a TextBlock bound to a property. I’m new to MVVM, and C# in general, so I’m not sure what I’m doing wrong.
In the end I have solved this problem, but I don’t understand why my solution works (always fun …), so I’d really appreciate your guidance.
In my app’s Model, I originally had something like this:
// Broken
namespace MyApp.MyModel
{
public class MetaData : INotifyPropertyChanged
{
private StatusType status;
public StatusType Status
{
get { return status; }
set
{
status = value;
statusMessage = ConvertStatusToSomethingMeaningful(value);
}
}
private string statusMessage;
public string StatusMessage
{
get { return statusMessage; }
private set
{
statusMessage = value;
// This doesn't work
NotifyPropertyChanged("StatusMessage");
}
}
...
}
}
Status is a enum, and when it’s set by my app, it also sets StatusMessage too (which is a more human readable description to show the user). My View’s TextBlock is bound to StatusMessage, but it doesn’t update using the above code.
However, if I move NotifyPropertyChanged("StatusMessage") into Status, my View’s TextBlock updates like it should. However, I don’t understand why this works when the original code above doesn’t?
// Fixed
namespace MyApp.MyModel
{
public class MetaData : INotifyPropertyChanged
{
private StatusType status;
public StatusType Status
{
get { return status; }
set
{
status = value;
StatusMessage = ConvertStatusToSomethingMeaningful(value);
// This works
NotifyPropertyChanged("StatusMessage");
}
}
public string StatusMessage { get; private set; }
...
}
}
Many thanks in advance for helping a newbie out 🙂
Issue in this line:
StatusMessage setter is never called (NotifyPropertyChanged(“StatusMessage”) called exactly there)
will be the right call
Probably my implementation of this would be next: