Refresh / Update WPF controls

Refresh / Update WPF controls

Sometime in the past,a friend asked me how to update a control to show status while his code is doing a loop of stuff. Essentially changing the text of a label (or sophisticatedly we can say a text-based progress bar). In my past coding with MFC and WinForms,it's fairly easy enough,you just invalidate and do an update (Invalidate / UpdateWindow in MFC or Invalidate / Update in WinForms). This approach also coincides with how Windows UI operate,where you specify the region that needs to be redrawn and then you send a message to the message pump for that control to paint itself.

So,I expected something similar (if not exactly the same) to also be present in WPF; much to my surprise,there is no equivalent. All my internet searches actually shows how to do this using background thread - it is the approach that needs to be taken in a proper programming context,however there are times when you just want to do something quick & dirty or you want to augment an existing app / port where you don't want to introduce new elements. There are also considerations to be made when both UI and worker thread access the same data,especially with regard to data binding (seemy postabout collection change not supporting multi-threading out of the Box).

So,I've decided to add a helper method to refresh a WPF control. I really appreciated the Refresh method in WinForms (which executes both Invalidate & Update),so I'm renaming my method to be Refresh as well. The code snippet below also show some C# specific techniques,namely: anonymous delegates and extension methods.

publicstaticclassExtensionMethods

{

privatestaticActionEmptyDelegate =delegate() { };

publicstaticvoidRefresh(thisUIElementuiElement)

{
uiElement.dispatcher.Invoke(dispatcherPriority.Render,EmptyDelegate);
}
}

privatevoidLoopingMethod()

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


The LoopingMethod is just the method I use in my Window class to update the label (updating the progress) and then the code does some heavy lifting (Sleep). The Refresh method is the extension method that takes any UI element and then calls that UIElement's dispatcher's Invoke method. The trick is to call the Invoke method with dispatcherPriority of Render or lower. Since we don't want to do anything,I created an empty delegate. So how come this achieves refresh functionality?

When the dispatcherPriority is set to Render (or lower),the code will then execute all operations that are of that priority or higher. In the example,the code already sets label1.Content to something else,which will result in a render operation. So by calling dispatcher.Invoke,the code essentially asks the system to execute all operations that are Render or higher priority,thus the control will then render itself (drawing the new content). Afterwards,it will then execute the provided delegate (which is our empty method).

Pretty weird; there was a post somewhere in my google search that led me this route,and I was surprised as to how it worked. I Couldn't find it anymore,but credit where credit is due,someone else figured out that Invoke-ing a Render or lower priority task will result in the UI being redrawn.

Update (January 20,2009):

A Commenter asked for a full sample,so I've uploaded onehere. I don't speak Spanish,butGoogle translatorwas working great!

Update (February 26,2009):

A Commenter asked for a VB.NET sample,so I've uploaded onehere.

Update (January 13,2010):

Apparentlygeekswithblogsdoesn't allow linking non-image files (thus the samples link doesn't work anymore). Samples are Now being put inmy Google Sites site. C# samplehereand VB.NET samplehere.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...