1

I created a custom Panel, within which I need to check whether its Children are managed by manipulating the collection manually, or whether the panel is used as ItemsPanel for an ItemsControl.

This to prevent the following exception from being thrown when trying to manipulate Children.

Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel.

How can I differentiate between the two different usages of the panel?

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
  • 1
    Can I ask why you are modifying the collection of children? Isn't the point of a **Panel** purely to layout its children by measuring and arranging them? – Steven Rands Mar 06 '15 at 16:17
  • @StevenRands That is a fair question, and I'm not fully certain it is a good idea yet either. Regardless, the scenario is I added the ability of specifying 'factories' to the panel ([search for 'LabelFactories' here](https://github.com/Whathecode/Framework-Class-Library-Extension/blob/master/Whathecode.PresentationFramework/Windows/Controls/AxesPanel.cs)) which manage recurrent labels which need to be displayed on the panel. When the panel is unloaded, I need to remove the labels added by the factories, but only when factories are used (as opposed to `ItemsSource`). – Steven Jeuris Mar 06 '15 at 16:52
  • OK, I see. I've used the panel's `OnRender` method to dynamically draw stuff like this in the past. It does have the drawback though that anything "rendered" in this way will appear _behind_ all of the children. Nevertheless there are situations where the approach is still useful. – Steven Rands Mar 06 '15 at 16:57

1 Answers1

1

A Panel's IsItemsHost property can be used to check whether the Panel "is a container for user interface (UI) items that are generated by an ItemsControl".

This property can be set manually when creating an ItemsControl template, but also seems to be set to true when using an ItemsPresenter and specifying the panel using ItemsControl's ItemsPanel property.

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161