Layout Controls: ItemsControl

The ItemsControl component is used to dynamically generate other UI controls. It does not place the elements on its own, it only creates them and adds them as children. The ItemsControl prefab uses a LayoutStackingPanel to manage the placement of the items it creates. ItemsControl has two important properties: Items and Item Template.

  • Item Template: this can be set to a prefab in the inspector. When the ItemsControl creates its children, it will create an instance of this prefab.
  • Items: cannot be edited in the inspector, but can be used as a target in Data Binding. The ItemsControl will create one instance of the ItemTemplate for each item in this IEnumerable. If the created instance has a Bindable component, the item that the instance was created for will be set as the Data Context.

ObservableCollection<T>

ObservableCollection implements IEnumberable<T>, IList<T>, and INotifyPropertyChanged with an added CollectionChanged event. When an element is added to, removed from, or swapped in (via List[index] = ) the backing List, CollectionChanged is fired. When an item is added or removed from the backing list PropertyChanged is fired for the Count property.

If the Items property of an ItemsControl is bound to an ObservableCollection, the ItemsControl will attach to the CollectionChanged event and dynamically update the children (create or destroy) when items are added to or removed from the backing ObservableCollection.

This entry was posted in Layout Controls. Bookmark the permalink.