I am Silverlight newbie and I can't make the simple Silverlight binding sample work!
I need to make a view-model, that shows the number of documents in the list, while it is loading.
I made a base-class, that implements INotifyPropertyChanged:
public abstract class BaseViewModel : INotifyPropertyChanged {
protected BaseViewModel() {}
#region INotifyPropertyChanged Members
protected void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
I made a child-class, that has "CountDocs" property:
public class DocumentViewModel : BaseViewModel {
public DocumentViewModel () {
...
}
...
public int CountDocs {
get { return countDocs; }
set {
if (countDocs != value) {
countDocs = value;
OnPropertyChanged("CountDocs");
}
}
}
public int countDocs;
}
I have DocumentViewModel.xaml with the following contents:
<UserControl
...
xmlns:vm="clr-namespace: ... .ViewModels" >
...
<UserControl.Resources>
<vm:DocumentViewModel x:Key="viewModel"/>
</UserControl.Resources>
...
<TextBlock x:Name="CounterTextBlock" Text="{Binding Source={StaticResource viewModel}, Path=CountDocs}"></TextBlock>
That is I mentioned namespace of my child-class, I made a resource of my child class with key "viewModel", and I entered binding of textblock, to this object's property "CountDocs".
The problem is that the CountDocs property fills the TextBlock only once: on load. But then I set CountDocs, and it does not fill the TextBlock.
I have tried to use the Mode property of binding, to use DataContext, but I still can't make it work.
Is there something wrong with the binding? How to make the ViewModel update when the CountDocs property of my object is changed?
Thanks