I have a DataGrid in my WPF application. For this DataGrid I have set ItemSource as below
ItemsSource="{Binding Path=MyItems}"
MyItems is List of MyItem declared in MyViewModel
// ItemsSource for MyItem Grid
public List<MyItem> MyItems {get;set; }
MyItem has set of Properties, Each property is mapped with each column in DataGrid. At initialization I am filling MyItems; So DataGrid is showing MyItems values in each column.
In a button command, I am creating a Worker thread, Worker thread modifies properties of MyItems which are shown in the Grid. For example MyItem has a property called "Status" which is binded to a column of grid. The status value is changed in the workerthread. End of the worker thread I am calling
OnPropertyChanged("MyItems"); //force UI refresh
But status column values in the grid is not updating.
Once I click the datagrid column then only the values are refreshing. How to refresh datagrid from worker thread?
If you do it right, you won't need to tell the grid to refresh. There are also unintended consequences if you did manage to refresh the whole grid, such as jumping to the top of the grid and loosing any user selected rows/cells as well as aborting edits if applicable.
This is how you should set up the list the grid is looking at:
private readonly ObservableCollection<MyItem> _myItems = new ObservableCollection<MyItem>();
public IEnumerable<MyItem> MyItems { get { return _myItems; } }
That will mean that new items, and removals update the grid automatically. However to update an individual cell, that property needs to be observable, i.e. the class MyItem
must implement INotifyPropertyChanged
.
For multithreading you'll need to set the properties on MyItem
in the UI thread, even if you do the calculation in a worker thread (see below). You can achieve this with a Dispatcher
. See Updating GUI (WPF) using a different thread
As for starting your own thread, do not do that, use a Task
.