I have a Windows Store project as follows:
class MyModel
{
private int _testVar;
public int TestVariable
{
get { return _testVar; }
set
{
_testVar = value;
NotifyPropertyChanged("TestVariable");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
My binding is as follows:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Path=TestVariable}" />
<Button Click="Button_Click_1"></Button>
</Grid>
And the code behind:
MyModel thisModel = new MyModel();
public MainPage()
{
this.InitializeComponent();
thisModel.TestVariable = 0;
DataContext = thisModel;
}
As this point, the binding appears to work as I get the textblock showing a 0. However, when I handle the button click event as follows:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
thisModel.TestVariable++;
}
I don’t see the number increasing. What am I missing here?
It seems that your class doesn’t implement
INotifyPropertyChanged.I mean I expected to see
class MyModel : INotifyPropertyChanged