如何在WinRT中使用INotifyPropertyChanged? [英] How do I use INotifyPropertyChanged in WinRT?

查看:68
本文介绍了如何在WinRT中使用INotifyPropertyChanged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,只是学习了 DataContext 和MVVM模型的基础知识。我现在有一个网格绑定到实现 INotifyPropertyChanged 的视图模型对象上,但是看来 UpdateSourceTrigger (其中所有WPF教程都告诉我使用),不适用于WinRT / Metro Style应用!

I'm a total newbie, just learning the basics of DataContext and the MVVM model. I've now got a grid bound to a view model object which implements INotifyPropertyChanged, however it appears that UpdateSourceTrigger (which all the WPF tutorials tell me to use) is not available for WinRT / Metro Style apps!

那我该如何实现INotifyPropertyChanged?

How do I implement INotifyPropertyChanged then?

我在这里已经束手无策了。我几乎一整天都在最基本的应用程序示例上花费了很多时间,只是想在单击某些内容后尝试更新网格。到目前为止,我设法做到的唯一方法是创建视图模型的全新实例,并重新分配我知道 DataContext 是错误的

I'm at the end of my tether here. I've spend nearly the whole day on the most basic of app examples, simply trying to get a grid to update after I click something. The only way I've managed to do this so far is to create an entirely new instance of the view model and reassign the DataContext which I know is wrong

更新:

我已经取得了一些进步,但是事情变得很奇怪。我有一个视图模型,带有通用的项目列表。项目列表通过PropertyChangedEventHandler连接。如果我用新集合替换整个集合,则列表视图将更新。

I have made some progress, but things have gotten very weird. I have a view model, with a generic list of items. The items list is wired up with a PropertyChangedEventHandler. If I replace the entire collection with a new one, the listview updates.

model.Items = new List<DataItem>{ new DataItem{ Title = "new item" }};

这将产生一个包含上述项目的项目列表。但是,如果我尝试添加项目,则什么也没发生

This results in a one item list with the above item. However, if I try adding an item, nothing happens

model.Items.Add(new DataItem{ Title = "added item" });

我还尝试创建一种添加项并专门触发PropertyChanged的方法,但这也没有工作

I also tried creating a method which added an item and specifically fired PropertyChanged, but that also doesn't work

在这里很奇怪。接下来,我尝试了这段代码。

Here's where it gets weird. Next I tried this code.

model.Items.Add(new DataItem { Title = "added item" });
model.Items = new List<DataItem> { new DataItem { Title = "new item" }}; 

这将产生两个项目列表:

This results in a two item list:

- new item
- added item

怎么会这样?代码说,添加一项然后替换整个列表,但执行顺序相反?

How can this be? The code says, "add one item" then "replace the whole list" but it executes in the reverse order?

更新2:

我已经按照建议的方式切换到ObservableCollection,实际上已经解决了原始问题。我现在可以添加一个项目,它显示在列表中。

I've switched to ObservableCollection as suggested, which has actually solved the original problem. I can now add an item and it shows up on the list.

但是,新的怪异行为仍然有效。在重置集合之前添加的项目将追加到新集合的末尾。为什么我的代码以相反的顺序执行?

However, the new weird behaviour is still in effect. Items added before the collection is reset are appended to the end of the new collection. Why is my code executing in reverse order?

推荐答案

您需要实现接口并在您关心更改的给定属性后发送通知。

You need to implement the interface and send out the notification once the given property you care about changes.

        public event PropertyChangedEventHandler PropertyChanged;

        public string CustomerName
        {
            get
            {
                return this.customerNameValue;
            }

            set
            {
                if (value != this.customerNameValue)
                {
                    this.customerNameValue = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("CustomerName"));
                    }
                }
            }
        }

保持请记住,对于集合,您应该使用 ObservableCollection 请注意在添加或删除项目时触发的 INotifyCollectionChanged

Keep in mind that for a collection, you should use an ObservableCollection as it will take care of the INotifyCollectionChanged being fired when an item is added or removed.

我建议您缩放样本尽可能地返回。不要以 DataGrid 开头,而是以简单的 TextBox Button ,其中 Button 强制更改ViewModel,然后将其反映在UI上。

I would suggest to scale your sample back as far as possible. Don't start with a DataGrid but rather a simple TextBoxand Button, where the Button forces a change in your ViewModel which will then reflect on the UI.

这篇关于如何在WinRT中使用INotifyPropertyChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆