How to update the Rendering of a WPF Container as new childs are being added?

muik

Member
Joined
Dec 3, 2009
Messages
13
Programming Experience
3-5
Hi everyone,

First I'd like to thank you all for this great forum. I used to code VB6 several years ago and this forum helped me transition to the new .NET framework without much pain - I could always find an answer to my questions. However, I cannot seem to find a VB solution to this problem and I'm counting on you guys to answer this first post of mine ;)

I have a WrapPanel container that I use to display as much as a thousand interactive buttons. When the user clicks "refresh", the application retrives data from several text files, creates the buttons in memory and add them to the WrapPanel. This is done in a While loop using the WrapPanel.Child.Add method. The refresh routine is launched as a separate thread using a Dispatcher.

Even though the application seems more responsive thanks to the Thread, I cannot seem to find a way to update/render the WrapPanel as new childs are added to it. In a perfect world, I would like the child elements to appear one at a time as they are being added to the WrapPanel. The current behavior is that the WrapPanel is cleared, and after 2 or 3 seconds, the thousand elements all pop-up simultaneously.

Some solutions I tried:
  • Launch the Thread with a higher priority (equal, below and over "Render" priority as well as AppIdle priority);
  • Use a progress bar to show that it's still alive (unfortunately its behavior was the same as the WrapPanel, and its "Render" only occured at the very beginning (empty bar) and very end (filled up bar));
  • Additional/manual calls to the WrapPanel's methods (UpdateLayout).

Can anyone point toward the right solution? I am puzzled, as this is the first time I have been involved with threads. I can think of a few solutions right now, but I might be very wrong in my understanding of the concepts:

  • Tell the WrapPanel that it needs to use some fancy animation (I know nothing about animations so I'm not even sure this is related);
  • Have the WrapPanel be in a separate thread than the WPF Window it's contained within, to get rid of the "Dispatcher" (which I'm using based on forum codes, but I don't really understand);
  • Find a way to have to WrapPanel.Children.Add call done outside of a While loop.

Here's how I create my thread:

VB.NET:
Private Sub Refresh()
    MagicContainer.Children.Clear() 'MagicContainer = WrapPanel
    
    Dim MyDispatcher As System.Windows.Threading.Dispatcher = Windows.Threading.Dispatcher.CurrentDispatcher
    MyDispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, New RefreshDelegate(AddressOf RefreshWeeklyView))
End Sub

Here's the essential of the refresh routine:

VB.NET:
Private Delegate Sub RefreshDelegate()
Private Sub RefreshWeeklyView()
'<...various preparation code here...>
    dim myButton as Button
    For x = 0 To LDAPs.Count - 1
        myButton = GetButton(x)
        MagicContainer.Children.Add(Incident)
    Next
End Sub

I hope I provided enough information. Thanks to anyone reading this, and 2 thumbs up for anyone who replies. :)
 
Back
Top