ObservableCollection问题 [英] ObservableCollection issue

查看:115
本文介绍了ObservableCollection问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个无法解决的问题,尽管看到了数十个样本.

问题是:更改ObservableCollection中的项目.
我可以看到UI更改的唯一方法是实现以下

Hi,

I have a problem which I cannot resolve although saw tens of samples with the issue.

and the problem is: changing an item within an ObservableCollection.
The only way I could see changes in the UI is by implementing the following

ObservableCollection<Item> temp = new ObservableCollection<Item>();
foreach (Item item in Items) temp.Add(item);
Items.Clear();
foreach (Item item in temp) mVideoOut.Add(item);




我以这种方式整理"了收藏.我知道这不是以这种方式实现的明智方法,但是我无法弄清楚INotify问题.也就是说,如果Item是从INotify派生的,那么如何通知集合.

有人可以帮我解决这个问题.

问候,

Igal




TYhi way I have "refreshed" the collection. I know it is not a smart way to implement it this way but I could not figure out the INotify issue. That is, if Item is derived from INotify how the collection is notified.

Can someone help me on this issue.

Regards,

Igal

推荐答案

我认为您的要求是在直接分配诸如mVideoOut = Items之类的集合时刷新UI.如果是这样,请为mVideoOut创建一个普通属性,然后在setter中,使用INotifyPropertyChanged为集合更改已更改的属性,或者可以使用Dependency属性.

弄清楚是否还有其他问题.
I think your requirement is to refresh the UI when you directly assign your collection like mVideoOut = Items. If so, either create a normal property for mVideoOut and in the setter, raise property changed for the collection using INotifyPropertyChanged or alternatively you can use Dependency property.

Clarify if you have some other problem.


ObservableCollection具有CollectionChanged事件.每当ObservableCollection更改(任何项目更改)时,您都可以跟踪此事件并更新您的控件.

http://stackoverflow.com/issues/1427471/c-observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyp [ http://social.msdn.microsoft.com/论坛/en-US/wpf/thread/8e9ef79d-e6cd-4cca-9934-24410dea522e [
An ObservableCollection has CollectionChanged Event. You can track this event and update your control whenever the ObservableCollection changed (Any item changed).

http://stackoverflow.com/questions/1427471/c-observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyp[^]

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8e9ef79d-e6cd-4cca-9934-24410dea522e[^]




HTH


我发现我的概念不正确.
实际上,我没有考虑更新的整个过程.

UI通过 Path 绑定到 ObservableCollection 内的对象.由于该对象的更改(添加,删除等)和项目中的更改(如果该项目),通过 ObservableCollection 通过两条路径(我当时只想过一条)来更新UI实现 INotifyPropertyChanged ).

我以为神奇地"通知 ObservableCollection 内部项目已更改,然后通知UI,UI随后通过 Path 并从项目.

但是实际上 ObservableCollection 不在画面中,有人必须调用项目(属性)的 set 命令,然后再调用OnPropertyChanged(在 ViewModelBase中实现.cs ),然后用户界面调用 get .

I have figured out that my concept was incorrect.
Actually, I did not think through the whole process of updating.

Well the UI is bounded to an object inside an ObservableCollection via the Path. The UI is updated via two routes (I was thinking only one) via the ObservableCollection as a result of changes on this object (add,remove, etc.) and via changes in the item (if the item implements INotifyPropertyChanged).

I was thinking that "magically" the ObservableCollection is notified that an internal item was changed and then it notifies the UI which then goes via the Path and reads the new value from the item.

But Actually the ObservableCollection is out of the picture and someone has to invoke the set command of the item (property) which then invokes the OnPropertyChanged (as implemented in ViewModelBase.cs) and then the UI invokes the get.

public int Item
{
    get { return mItem; }
    set
    {
        mItem= value;
        OnPropertyChanged("Item");// taken from ViewModelBase.cs
    }
}



这是谁?好吧,它可能是绑定到另一个UI(例如按钮,滑块)的另一个属性,然后从其 set 中调用 set .或者,就像我在Mosel本身发生变化(正在进行一些通信)的情况下一样,是通过实现 DispatcherTimer 来实现的,它定期读取模型中的参数并更新用于通知UI的属性. br/>
的确,我可以实现从Model到ModelView的事件通知,但对我来说MVVM听起来不对(我要遵循).

伊加尔(Igal)



Who is this someone: well it could be another property which is binded to another UI (eg button,slider) and then call the set from within its set. Or, as I did in case of changes in the Mosel itself (some communication going on there) is by implementing a DispatcherTimer which periodically reads a parameter in the model and updates the property which notifies the UI.

It is true that I could implement an event notification from the Model to the ModelView but it just does not sound to me MVVM (which I am trying to follow).

Igal


这篇关于ObservableCollection问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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