Reputation: 9926
Have class behine the xaml that contain the propertie 'Int32 Count'
I want to bind some TextBlock to the 'Count' value - that the TextBlock.Text will have the value of the 'Count'.
So i wrote in the xaml :
<TextBlock Text="{ Binding Path=Count }" />
And in the code behind the xaml i add to the constructor:
DataContext = this;
But each change of the 'Count' does not change the text of the TextBlock.
The code of 'Count'
Int32 count;
public Int32 Count
{
get
{
return count;
}
set
{
count = value;
}
}
Upvotes: 0
Views: 169
Reputation: 547
place the INotifyPropertyChanged interface to your class:
public class MainPage : INotifyPropertyChanged
{
}
then implement:
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
what this does if provide the mechanism for notifying the view that something has changed in your datacontext and you do it like this:
public Int32 Count
{
get
{
return count;
}
set
{
count = value;
OnPropertyChanged("Count"); //This invokes the change
}
}
But of course, i recommend that you separate design and code using the MVVM pattern. This way, you can implement the propertychanged to a ViewModelBase class, then inherit that for each of your ViewModels.
Upvotes: 4
Reputation: 33252
You should ensure your DataContext implements INotifyPropertyChanged. Then you have to fire the property change events properly.
Upvotes: 0