Refresh / Update WPF controls like win forms

隐身守侯 提交于 2019-12-05 07:21:46

问题


changing the text of a label (or sophisticatedly we can say a text-based progress bar). in winforms you just Invalidate / Update.

But how to do this in WPF without using Background Threads. ???


回答1:


    public static class ExtensionMethods
{

   private static Action EmptyDelegate = delegate() { };


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

private void LoopingMethod()
{
   for (int i = 0; i < 10; i++)
   {
      label1.Content = i.ToString();
      label1.Refresh();
      Thread.Sleep(500);
   }
}

Reference: http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx




回答2:


Perhaps you should read more on the subject of Bindings..

Basicly, bindings will manage this for you..



来源:https://stackoverflow.com/questions/1831373/refresh-update-wpf-controls-like-win-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!