Search code examples
c#wpfwinformsinvalidation

Refresh / Update WPF controls like win forms


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. ???


Solution

  •     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: Link