ListBox control styling tips
The ListBox control represents a collection of items. You can populate a ListBox control by binding it to a data source or by displaying unbound items. The ListBox control is an items control, which means that you can populate it with items that contain text or other controls.
As with all controls, the ListBox control (and each ListBoxItem control contained in the ListBox object) can be modified to look very different from the default appearance. By default, the ListBox control looks like the following:
You can set the item that is selected in the ListBox control by setting the SelectedIndex property under Common Properties in the Properties panel. A value of -1 displays the ListBox object without any selection made. A value of 0 displays the ListBox object with the first item selected.
To add items to a ListBox control, you can manually add ListBoxItem objects, or you can bind a collection of data to the ListBox object to make items appear automatically.
The ListBox control uses one template to define its appearance: the ListBox Template. The single part of this template plays a role in the appearance and behavior of the ListBox object to which the template is applied.
To style the items that a ListBox control contains, you have to modify other templates. For example, when a ListBox control is data-bound, the template that it uses to style the items it contains is a generated items template (a data template). When a ListBox control is not data-bound, it does not have a template that it applies to the items it contains. Instead, you can modify the template of the items themselves, such as the ListBoxItem template.
Other objects can exist in the template to dress up the appearance of the ListBox control, but the part listed in the following table is tied to its behavior in a contract.
To view the parts of a template, open the Parts panel while modifying the template. In the Objects and Timeline panel, an icon appears next to an object that is playing the role of a part in the Parts panel.
An object that displays the items in a scrollable window.
This part is mandatory.
The ScrollViewer object in the ListBox template can have a modified template applied to it. The ScrollBar objects of the ScrollViewer object can also have modified templates applied to them. This results in templates that are nested in the following way:
A ListBox control. The ListBox control is a container for a list of items, either bound to items in a data source, or displaying individual unbound objects.
A ScrollViewer control inside the ListBox template.
An unbound item in the ListBox control.
A vertical ScrollBar control inside the ScrollViewer template.
A vertical Thumb control inside the ScrollBar template.
A vertical RepeatButton control inside the ScrollBar template to decrease the value of the ScrollBar control in large increments.
A vertical RepeatButton control inside the ScrollBar template to decrease the value of the ScrollBar control in small increments.
By default, the ListBox control can be in one of the following three states of the ValidationStates state group:
The appearance of the ListBox control when it is valid.
The appearance of the ListBox control when it is invalid and does not have keyboard focus.
The appearance of the ListBox control when it is invalid and has keyboard focus.
When you select a state, state recording is turned on, and any changes that you make will be recorded for that state. To turn off state recording, click the recording button on the artboard, or select Base in the States panel.
To modify the template of a ListBox control, do one of the following:
Draw a ListBox control on the artboard, and then create a copy of the default template.
For more information, see Create or modify a template.
Design the look of your ListBox control by drawing objects or importing art, and then using the Make Into Control command.
If you use the Make Into Control command, follow these steps to make sure that you create all the objects that the ListBox template requires:
Group all the objects that you want to define the appearance of your ListBox control into a Grid control. Include those objects that are not destined to become parts of the template.
Select the new Grid control, and then, on the Tools menu, click Make Into Control.
In the dialog box that appears, select ListBox, name your template, and select the location where the template will be stored.
For information about locations, see Create a resource.
After you click OK, Microsoft Expression Blend enters template-editing mode and displays the objects that make up your ListBox control. You can continue to modify your template in this mode. For example, add or modify objects, or select a state in the States panel to modify the appearance of your template in that state.
To create the ScrollViewer part, double-click ScrollViewer in the Parts panel. With the new ScrollViewer object selected, draw an ItemsPresenter control inside it. Use the Selection tool or the properties under Layout in the Properties panel to resize and move these objects inside your template.
Consider binding some of the brush properties of objects in your template to the following properties of the ListBox object that will eventually use your template:
For more information, see Carry object properties through to the template.
To exit template-editing mode, click [ListBox] in the breadcrumb bar at the top of the artboard, or click Return scope to in the Objects and Timeline panel.
For information about applying your new ListBox template to other ListBox objects, see Apply or remove a resource.
You can find detailed information about the properties and events of the Microsoft Silverlight ListBox control at the Silverlight on MSDN.