Content Model: Adding a child to an ItemsControl object implicitly adds it to the ItemCollection for the ItemsControl object. For more information on the content model for ItemsControl, see Controls Content Model Overview.
Note that you use either the Items or the ItemsSource property to specify the collection that should be used to generate the content of your ItemsControl. When the ItemsSource property is set, the Items collection is made read-only and fixed-size. This means that you cannot add items to the collection directly.
When ItemsSource is in use, setting the property to nullNothingnullptra null reference (Nothing in Visual Basic) removes the collection and restores usage to Items, which will be an empty ItemCollection.
Each ItemsControl type has a corresponding container type. Container elements are the objects that contain the data items in the item collection. For example, for ListBox, the generated containers are ListBoxItem controls; for ComboBox, they are ComboBoxItem controls.
The ItemsControl generates its items through the IItemContainerGenerator interface. The ItemContainerGenerator property of the ItemsControl is of type ItemContainerGenerator, which implements the IItemContainerGenerator interface. Therefore, you can access the ItemContainerGenerator object associated with your ItemsControl using the ItemContainerGenerator property. For example, if you have a data-bound TreeView and you want to get a TreeViewItem based on its index or its associated data item, you can use the ItemContainerGenerator..::.ContainerFromIndex or the ItemContainerGenerator..::.ContainerFromItem method. Alternatively, you can use the ItemContainerGenerator..::.IndexFromContainer or the ItemContainerGenerator..::.ItemFromContainer method to get the index or data item associated with a given generated container element.
Apart from those usages, the IItemContainerGenerator interface is used in advanced scenarios. Typically, advanced applications that have their own implementation of a virtualizing panel call members of the interface.
Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.