Datagrid does not update after data change

advertisements

I have a WPF DataGrid bound to a ObservableCollection via dg.ItemsSource = myCollection;

The MyClass implements INotifyPropertyChanged. I have a calculated property which is calculated async.

The property is a SolidColorBrush bound to the color of the Text. After the value of the property changes the DataGrid does not update immediately, but it updates as soons as I select the line of the item of which the property changed it display the updated value.

What can I do to let the DataGrid update immediately? I can't use XAML binding for the ItemsSource.

Binding:

<DataGridTextColumn Foreground={Binding Path=MyBrushProperty} />

Code of the class:

public class MyClass : INotifyPropertyChanged
{
public SolidColorBrush MyBrushProperty {
   get {
      CalcMyBrush();
      return _myBrushProperty;
   }
  set{
   _myBrushProperty = value;
   OnPropertyChanged("MyBrushProperty");
  }
}
private void CalcMyBrush(){
   //DoSomething that changes _myBrushProperty and takes some time and needs to run async and after that sets
   MyBrushProperty = somevalue;

}
}


I think you need to rise PropertyChanged event for your calculated property after you got async result of calculation.

something like this

public SolidColorBrush MyBrushProperty{get{return myBrushProperty;}}
private void CalcMyBrush()
{
    var awaiter = YourAsyncCalculation.GetAwaiter();
    awaiter.OnComplited(()=>
    {
     myBrushProperty= (SolidColorBrush)awaiter.GetResult();
     OnPropertyChanged("MyBrushProperty");
    });
}