更新WPF Datagrid中的单行 [英] Update single row in a WPF Datagrid
问题描述
我正在创建一个下载管理器,我的WPF数据网格绑定到了代表正在进行的下载的对象集合(在单独的线程中)。当我有多个下载运行时,每个下载人都使用此代码每秒更新其datagrid项:
I'm creating a download manager, and my WPF datagrid is bound to a collection of objects representing ongoing downloads (in separate threads). When I have multiple downloads running, each one is using this code to update its datagrid item every second:
if (DateTime.Now > download.LastUpdateTime.AddSeconds(1))
{
this.downloadsGrid.Items.Refresh();
download.LastUpdateTime = DateTime.Now;
}
Datagrid.Items.Refresh()可以完成工作,但可以重建整个工作datagrid,导致所有下载在一秒钟内多次更新彼此的datagrid行,我不想这种行为。有什么方法可以刷新数据网格中的特定行(项目)?
Datagrid.Items.Refresh() does the job, but it reconstructs the whole datagrid, causing all downloads to update each others datagrid rows several times in one second, and I don't want that kind of behavior. Is there any way to refresh a specific row (item) in a datagrid?
推荐答案
如果将DataGrid绑定到 ObservableCollection (它实现了 INotifyCollectionChanged ),当添加新项或删除某项时,您的DataGrid将会收到通知。此外,如果您只是更新集合中某个对象的属性,则该对象应实现 INotifyPropertyChanged 并引发PropertyChanged事件,该事件将告诉DataGrid仅更新该值。
If you bind your DataGrid to an ObservableCollection (which implements INotifyCollectionChanged) your DataGrid will be notified when a new item is added or an item is remove. Additionally, if you're just updating a property on an object in the collection the object should implement INotifyPropertyChanged and raise the PropertyChanged event which will tell the DataGrid to just update that value.
这篇关于更新WPF Datagrid中的单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!