[英]WPF Update UI From RefreshEvent
I actually try to update the MainWindow UI from a RefreshEvent in a external class. 我实际上尝试从外部类中的RefreshEvent更新MainWindow UI。
I tried out the following, but the UI doesnt refresh. 我尝试了以下操作,但是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()
应该可以解决问题
to be sure your ui is refreshed you should use Dispatcher 确保您的用户界面刷新,您应该使用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 然后yourElement.Refresh()将解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.