ItemsControl Customization

One of the cool things about ItemsControls is that you get to separate the data binding and item control logic from the layout logic by specifying the Panel that you want to lay items out.

There are two ways of specifying a Panel for an ItemsControl. One of them is to use the ItemsPanel property, which allows you to specify an ItemsPanelTemplate that is used to instantiate the Panel of choice.

The other method is to have a Panel in the Template for the ItemsControl, and set IsItemsHost to true. This will tell the ItemsControl that this Panel is the one who is hosting the Items for the ItemsControl and laying them out.

The reason why this is so cool is because it allows the application developer to separate out many aspects of a control into separate reusable pieces, each of which is not too tightly bound to the other. For example, the WPF ListBox does not have to stack items vertically, or have every item the same size. It can use any Panel to lay out the items, because what makes it a ListBox does not involve the item layout. Try setting the Panel of a ListBox to a WrapPanel (or some other panel that lays out items automatically) to see this in action.