如何更新一个ObservableCollection的现有元素? [英] How do I update an existing element of an ObservableCollection?

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

问题描述

我有绑定到一个WPF列表框具有两个单独的数据模板(一个用于显示,一个用于编辑)的ObservableCollection的一个实例。要编辑的数据模板具有一个单向的文本框结合,和一个保存按钮。

I have an instance of ObservableCollection bound to a WPF listbox with two separate data templates (one for display, one for editing). The data template for editing has a one-way binding on the textbox, and a Save button.

什么样的​​变化,我需要作出这样,当我preSS Save按钮(把列表项在编辑模式后),该值我改变文本框取代了的ObservableCollection值(和显示) ?

What changes do I need to make so that when I press the Save button (after putting the list item in edit mode), the value I change the textbox to replaces the value in the ObservableCollection (and the display)?

推荐答案

您的收藏物品应实现INotifyPropertyChanged接口类型。这样,你的列表框中会通知你的单项对象的属性值已更改。的ObservableCollection引发CollectionChanged事件只有当收集的变化(项目添加,删除等)

Items in your collection should be of type that implements INotifyPropertyChanged interface. This way your list box will be notified that property value in your single item object has changed. ObservableCollection raises CollectionChanged event only when collection changes (items added, removed, etc.)

从的ObservableCollection MSDN库文章引用

Quote from the MSDN library article on ObservableCollection

要全力支持数据传输
  从源对象绑定到值
  约束性指标,在每一个你的对象
  集合,支持绑定
  属性必须实施
  相应的属性改变
  通知机制如
  INotifyPropertyChanged接口。

To fully support transferring data values from binding source objects to binding targets, each object in your collection that supports bindable properties must implement an appropriate property changed notification mechanism such as the INotifyPropertyChanged interface.

这篇关于如何更新一个ObservableCollection的现有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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