Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Update UI From RefreshEvent

Tags:

c#

wpf

dispatcher

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();
           }
       }
 }));
like image 446
Phil795 Avatar asked Jun 21 '16 08:06

Phil795


2 Answers

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

like image 54
Belterius Avatar answered Sep 28 '22 19:09

Belterius


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

like image 20
Boo Avatar answered Sep 28 '22 20:09

Boo