Click here to Skip to main content
15,943,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the System.Windows.Forms.TreeView, the .Refresh() can be used to refresh the control. In the System.Windows.Controls.TreeView, how to refresh the control? Thanks.

What I have tried:

How to refresh System.Windows.Controls.TreeView?
Posted

1 solution

If you are talking about how to add data and refresh it after all data has been added, the standard technique is to call BeginUpdate(); just before you start adding data and EndUpdate(); after you have added the data. Once you have done this, call Refresh();. It would look something like this:
C#
private void PopulateTreeview(TreeView treeView)
{
  treeView.BeginUpdate();
  try
  {
    // Load data in this bit
  }
  finally
  {
    treeView.EndUpdate();
    treeView.Refresh();
  }
}
I use a try/finally to make sure I finish the update part, and refresh the control. This helps to cope with times when the act of loading data into the treeview throws an exception. I don't catch the exception because there's nothing I can do with it in here and I want to use good practices for handling exceptions.
 
Share this answer
 
Comments
s yu 3-May-24 15:18pm    
Pete: Thanks for your post. For System.Windows.Controls.TreeView, it can be refreshed by
.Items.Refresh()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900