0

Suppose I have this data grid:

<DataGrid x:Name="MyGrid" AutoGenerateColumns="True" ItemsSource="{Binding MyObservableList}"></DataGrid>

Suppose I have this Loop:

for (int i = 0; i < 10; i++)
{
  MyObservableList.Add(i.ToString());
  Console.WriteLine(i.ToString());
  Thread.Sleep(500);
}

Is possible to change the wpf list immediately after the item is inserted? Like "Console.Write" code? I tried lots of stuff like implementing "CollectionChanged" or "INotifyPropertyChanged" but they only update the DataGrid when the loop ends. Note: the objective is update the DataGrid from a ObservableCollection during the loop process. Any example with any kind of observable list is ok.

Freddy
  • 75
  • 7

1 Answers1

1

Don't sleep on the UI thread as it will block it from being able to update the UI. Wait asynchronously using a task:

for (int i = 0; i < 10; i++)
{
    MyObservableList.Add(i.ToString());
    Console.WriteLine(i.ToString());
    await Task.Delay(500);
}
mm8
  • 163,881
  • 10
  • 57
  • 88