簡體   English   中英

WPF更新UI從RefreshEvent

[英]WPF Update UI From RefreshEvent

我實際上嘗試從外部類中的RefreshEvent更新MainWindow UI。

我嘗試了以下操作,但是UI不會刷新。

 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();
           }
       }
 }));

調用((MainWindow)System.Windows.Application.Current.MainWindow).UpdateLayout()應該可以解決問題

確保您的用戶界面刷新,您應該使用Dispatcher

public static class UiRefresh
{

    private static Action EmptyDelegate = delegate () { };


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

然后yourElement.Refresh()將解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM