Update single row in a WPF Datagrid

佐手、 提交于 2020-03-19 05:08:48

问题


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() 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?


回答1:


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.




回答2:


Does your download class implement INotifyPropertyChanged? If not, that's why you have to call refresh to see a change in the grid. If you implement INotifyPropertyChanged the binding will be updated when the value is changed.



来源:https://stackoverflow.com/questions/10487690/update-single-row-in-a-wpf-datagrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!