Before I get to my question/problem, here is the basic structure of my application:
In my MainWindow.xaml (the main view in this case) I have a Label (I realize I should change this to a TextBlock, but that is a separate issue):
<Label Name="StatusLabel" Content="{Binding Path=Status}"/>
It’s corresponding ViewModel (MainWindowViewModel) contains another ViewModel (SiteListViewModel) and a Status property:
public class MainWindowViewModel : ViewModelBase
{
public SiteListViewModel SiteList { get; set; }
public String Status
{
get { return SiteList.Status; }
}
}
As you can see, MainWindowViewModel’s Status property returns the SiteListViewModel instances’ Status property, which is defined as such:
public class SiteListViewModel : ViewModelBase
{
private string status;
public String Status
{
get { return this.status; }
set
{
this.status = value;
base.OnPropertyChanged("Status");
}
}
}
SiteListViewModel’s Status property is updated in various places during asynchronous processes to keep the user informed as to what is going on. This is done with simple assignment calls such as:
Status = String.Format(Properties.Resources.SiteListViewModel_Status_LoadingJobs, count + 1, totalSites);
Another important note, Both MainWindowViewModel and SiteListViewModel inherit from ViewModelBase which implements INotifyPropertyChanged.
Now for the actual question/problem:
The label does not update.
However, if I change the binding in the view to this:
<Label Name="StatusLabel" Content="{Binding Path=SiteList.Status}"/>
It works fine. Which seems to indicate that I am missing something at the MainWindowViewModel level. I could leave it as it is, but that reaches into the structure of the child view model, which is not something I want to do.
Can someone point to what I am doing wrong?
Of course it does not update, the binding system listens to change events for the property you bind to on the type owning that property, you do not fire any notifications at all.
You could forward the notifications of the nested VM:
Where
OnPropertyChangedis a method to raisePropertyChanged; your VM base-class should have something like that.I would recommend dropping the property though, it’s redundant as you can bind directly though
SiteList(also that property should fire change notifications in its setter). Also you would need to detach the handler from the old instance and re-attach it to the new instance whenever theSiteListchanges (could be done in the setter as well).