How do I refresh visual control properties (TextBlock.text) set inside a loop?

前端 未结 5 956
醉梦人生
醉梦人生 2020-12-14 04:08

With each loop iteration, I want to visually update the text of a textblock. My problem is that the WPF window or control does not visually refresh until the loop is complet

5条回答
  •  长情又很酷
    2020-12-14 04:13

    I tried the solution exposed here and it didn't work for me until I added the following:

    Create an extension method and make sure you reference its containing assembly from your project.

    public static void Refresh(this UIElement uiElement){
        uiElement.Dispatcher.Invoke(DispatcherPriority.Background, new Action( () => { }));
    }
    

    Then call it right after RaisePropertyChanged:

    RaisePropertyChanged("MyValue");
    myTextBlock.Refresh();
    

    That will force the UI thread to take control for a small while and dispatch any pending changes on the UI element.

提交回复
热议问题