Xaml Windows应用商店绑定问题
我有一个Windows应用商店项目,如下所示:Xaml Windows应用商店绑定问题,xaml,binding,windows-8,windows-runtime,Xaml,Binding,Windows 8,Windows Runtime,我有一个Windows应用商店项目,如下所示: class MyModel { private int _testVar; public int TestVariable { get { return _testVar; } set { _testVar = value; NotifyPropertyChanged("TestVariable"); }
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));
}
}
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Path=TestVariable}" />
<Button Click="Button_Click_1"></Button>
</Grid>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
thisModel.TestVariable++;
}
我的约束力如下:
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));
}
}
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Path=TestVariable}" />
<Button Click="Button_Click_1"></Button>
</Grid>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
thisModel.TestVariable++;
}
此时,当我得到显示0的textblock时,绑定似乎起作用。但是,当我按如下方式处理按钮单击事件时:
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));
}
}
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Path=TestVariable}" />
<Button Click="Button_Click_1"></Button>
</Grid>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
thisModel.TestVariable++;
}
我看这个数字没有增加。我缺少什么?您的类似乎没有实现
INotifyPropertyChanged
。
我的意思是我希望看到
类MyModel:INotifyPropertyChanged
您的类似乎没有实现INotifyPropertyChanged
。
我的意思是我希望看到
类MyModel:INotifyPropertyChanged
首先,视图模型必须实现INotifyPropertyChanged,或者更好地使用某种MVVM库,例如,这将对您有很大帮助。其次,我不确定调用thisModel.TestVariable++是否真的更新了值?尝试使用thisModel.TestVariable=thisModel.TestVariable+1 首先,视图模型必须实现INotifyPropertyChanged,或者更好地使用某种MVVM库,例如,这将对您有很大帮助。
其次,我不确定调用thisModel.TestVariable++是否真的更新了值?尝试使用thisModel.TestVariable=thisModel.TestVariable+1 毫无疑问,++将引发PropertyChanged。我可以确认此模型。TestVariable++在我实现InotifyPropertyChnaged后工作正常。毫无疑问,++将引发PropertyChanged。我可以确认此模型。TestVariable++在我实现InotifyPropertyChnaged后工作正常。我不相信我错过了!真不敢相信我错过了!