0

I often insert/delete 20-30 very complicated UserControls into StackPanel. All UserControls already created and cached for reusing. Before reusing control I clean old parent and remember control.

Operations like StackPanel.Children.Remove, StackPanel.Children.Add take a lot of time in my scenarios.

How can I optimize these operations? E.g. insert all controls in StackPanel at a time or freeze notifications during collection's modifications or else?

May who known useful articles regrading wpf optimization (except MSDN)?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Victor
  • 3,497
  • 3
  • 39
  • 51

2 Answers2

2

You could override the OnCollectionChanged() event handler of your ObservableCollection, to either disable event handling all together, or to queue the events to handle later.

See the post here, the answer could help you - https://stackoverflow.com/a/5491534/903056

Community
  • 1
  • 1
EkoostikMartin
  • 6,831
  • 2
  • 33
  • 62
  • How I known StackPanel does't use ObservableCollection for internal implementation. It uses UIElementCollection and I don't have idea how I can customise this collection – Victor Jun 20 '12 at 22:44
  • I assumed the items in your StackPanel were bound to an ObservableCollection (as often done in MVVM). – EkoostikMartin Jun 21 '12 at 01:05
1

I assume you need to look into virtualized UI or data loading ( if any) strategies.

In this specific case, use controls like VirtualizingStackPanel to make the UI draw fewer things. You can have more information on this blog.

s_nair
  • 812
  • 4
  • 12
  • How I wrote in question, I use only ~20 controls in container. The problem is that my child controls very complicated. And I think that each insertion cause water flow notifications for already inserted controls. – Victor Jun 20 '12 at 22:48