Search code examples
wpfuser-controlsstackpanel

WPF optimization: Insert/Delete child from StackPanel


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)?


Solution

  • 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