7

I actually try to update the MainWindow UI from a RefreshEvent in a external class.

I tried out the following, but the UI doesnt refresh.

 System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate
 {
      foreach (OPCItem o in ((MainWindow)System.Windows.Application.Current.MainWindow).dgItems.Items)
      {
          if (o.ItemID == arg.items[i].OpcIDef.ItemID)
          {
              o.Value = Item.Value;
              o.DateTime = Item.DateTime;
              o.Quality = Item.Quality;

             ((MainWindow)System.Windows.Application.Current.MainWindow).dgItems.Items.Refresh();
           }
       }
 }));
Philipp Nies
  • 945
  • 4
  • 20
  • 38
  • 1
    Even if you get this to work, re-consider your design. The problem you're having is only a symptom of a bigger design problem. – ardila Jun 21 '16 at 08:56

2 Answers2

11

Calling ((MainWindow)System.Windows.Application.Current.MainWindow).UpdateLayout() should do the trick

Belterius
  • 738
  • 1
  • 10
  • 20
6

to be sure your ui is refreshed you should use Dispatcher

public static class UiRefresh
{

    private static Action EmptyDelegate = delegate () { };


    public static void Refresh(this UIElement uiElement)
    {
        uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
    }
}

then yourElement.Refresh() will do the trick

Boo
  • 663
  • 5
  • 10
  • When doing this on the mainwindow I had to put parameters in reverse order, like: this.Dispatcher.Invoke(delegate () { }, DispatcherPriority.Render); – DAG Sep 07 '20 at 20:34