繁体   English   中英

Windows应用商店绑定问题

[英]Windows Store Binding Issue

我有一个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));
    }


}

我的绑定如下:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="{Binding Path=TestVariable}" />
    <Button Click="Button_Click_1"></Button>
</Grid>

以及后面的代码:

    MyModel thisModel = new MyModel();

    public MainPage()
    {
        this.InitializeComponent();

        thisModel.TestVariable = 0;
        DataContext = thisModel;
    }

至此,当我使文本块显示为0时,绑定似乎可以正常工作。但是,当我按如下方式处理按钮单击事件时:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        thisModel.TestVariable++;
    }

我看不到这个数字在增加。 我在这里想念什么?

看来您的课程没有实现INotifyPropertyChanged 我的意思是我希望看到class MyModel : INotifyPropertyChanged

首先,视图模型必须实现INotifyPropertyChanged或更好地使用某种MVVM库,例如MVVM Light ,这将对您有很大帮助。
其次,我不确定,是否调用thisModel.TestVariable ++会真正更新该值? 尝试使用thisModel.TestVariable = thisModel.TestVariable +1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM