WPF 从 RefreshEvent 更新 UI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37939444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Update UI From RefreshEvent
提问by Phil795
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();
}
}
}));
回答by Belterius
Calling ((MainWindow)System.Windows.Application.Current.MainWindow).UpdateLayout()should do the trick
打电话((MainWindow)System.Windows.Application.Current.MainWindow).UpdateLayout()应该可以解决问题
回答by Boo
to be sure your ui is refreshed you should use Dispatcher
为确保您的 ui 已刷新,您应该使用 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() 会做的伎俩

