【发布时间】:2009-06-11 16:00:25
【问题描述】:
我通过 DataGrid.ItemSource 属性将 IEnumerable Collection 传递到 WPF DataGrid 中。但是当我尝试更改代码中的集合项时,它不会更新 DataGrid。 为什么?
【问题讨论】:
标签: wpf datagrid datagridview
我通过 DataGrid.ItemSource 属性将 IEnumerable Collection 传递到 WPF DataGrid 中。但是当我尝试更改代码中的集合项时,它不会更新 DataGrid。 为什么?
【问题讨论】:
标签: wpf datagrid datagridview
您需要绑定到实现 INotifyCollectionChanged 接口的类型,以便它提供数据绑定可用于监控何时添加或删除项目的事件。 WPF 中最好的类型是 ObservableCollection,它有一个可以接受 IEnumerable 的构造函数:
ObservableCollection<string> collection = new ObservableCollection<string>(iEnumerableobject);
dataGrid.ItemSource = collection;
collection.Add("Wibble");
会正确更新。
从您的 cmets 到另一个答案,您似乎需要从 UI 线程内部调用 add 调用。在不详细了解您的代码的情况下,我不知道您为什么需要这样做,但我们假设您正在后台从服务获取数据:
private ObservableCollection<string> collection;
public void SetupBindings()
{
collection = new ObservableCollection<string>(iEnumerableobject);
dataGrid.ItemSource = collection;
//off the top of my head, so I may have this line wrong
ThreadPool.Queue(new ThreadWorkerItem(GetDataFromService));
}
public void GetDataFromService(object o)
{
string newValue = _service.GetData();
//if you try a call add here you will throw an exception
//because you are not in the same thread that created the control
//collection.Add(newValue);
//instead you need to invoke on the Ui dispatcher
if(Dispather.CurrentDispatcher.Thread != Thread.CurrentThread)
{
Dispatcher.CurrentDispatcher.Invoke(() => AddValue(newValue));
}
}
public void AddValue(string value)
{
//because this method was called through the dispatcher we can now add the item
collection.Add(value);
}
正如我所说,我手头没有 IDE,所以这可能无法编译,但会为您指明正确的方向。
不过,根据您在后台执行的具体任务,可能会有更好的方法来执行此操作。我上面的例子使用backgroundworker 会更容易实现,所以你可能也想了解一下。
【讨论】:
您需要改用 ObservableCollection。 (或者让你自己的类包装集合并实现 INotifyPropertyChanged 接口)
【讨论】:
如果由于某种原因不能使用 ObservableCollection,也可以使用实现 INotifyCollectionChanged 接口的集合...
【讨论】: