Luke’s Guide to the ListBox Control

Tuesday, January 26, 2010 / Posted by Luke Puplett /

This post is a copy (I'd cry if it got mauled) of the Community Content I added to the ListBox MSDN article explaining its highly complex model.

Every time I go to sleep I forget this, may I say, overly complicated beast of a control. Here's some community content that might help programmers of a similar disposition.

Right, a ListBox is a bunch of behaviours over the top of an ItemsControl which is essentially a controller for items in a Panel.

It's useful to consider its template which consists of a Border, a ScrollViewer (of which the ScrollViewer has a ScrollContentPresenter) and an ItemsPresenter.

This Panel can be changed via ItemsControl.ItemsPanel but could also be specified in the control template by adding your chosen Panel and setting its IsItemsHost=true which will hook it up to the ItemsControl/ListBox.

The ItemsControl's job is to generate the items according to customizable templates using an ItemContainerGenerator. For a ListBox, this makes ListBoxItem instances which are ContentControls and thus have ContentPresenters in their templates. Their look is affected using...

ItemTemplate. Simple except that it should not be used in conjunction with ItemTemplateSelector (which sets a DataTemplateSelector class that returns a DataTemplate when its SelectTemplate method is call and so you can add some kind of logic to determine which one to return).

And we're not done yet because there's also ItemContainerStyle and ItemContainerStyleSelector which do a similar job except that they style the container (ListBoxItem) around the item's actual rendered content (as defined by ItemTemplate).

As for the look of the highlight, I guess that's done as part of the template applied to the item in reaction to ListBoxItem.IsSelected. Probably.

[sigh] Whatever happened to HTML and CSS eh?

Labels: ,


Post a Comment