[英]Windows Store Binding Issue
I have a Windows Store project as follows: 我有一个Windows Store项目,如下所示:
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: 至此,当我使文本块显示为0时,绑定似乎可以正常工作。但是,当我按如下方式处理按钮单击事件时:
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
. 看来您的课程没有实现INotifyPropertyChanged
。 I mean I expected to see class MyModel : INotifyPropertyChanged
我的意思是我希望看到class MyModel : INotifyPropertyChanged
First, the view model must implement INotifyPropertyChanged or better use some kind of MVVM Library like MVVM Light , this will help you a lot. 首先,视图模型必须实现INotifyPropertyChanged或更好地使用某种MVVM库,例如MVVM Light ,这将对您有很大帮助。
Second, I'm not sure, if the call thisModel.TestVariable++ actually updates the value? 其次,我不确定,是否调用thisModel.TestVariable ++会真正更新该值? Try to use thisModel.TestVariable = thisModel.TestVariable + 1; 尝试使用thisModel.TestVariable = thisModel.TestVariable +1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.