Style a control that displays data

Visual Studio 2012

Data is displayed by binding items of data to objects on the artboard. You can bind items of data to the properties of any object, whether you're binding a string value that represents the hexadecimal value of a color to the Background property of a rectangle, or binding a collection of data items to the ItemsSource property of a ListBox.

To style an object that displays data, you can modify the properties of the object, or, if the object supports templates, you can modify one of the templates that are applied to the object.

When you select an object on the artboard, you can modify values in the Properties panel to change the appearance and other aspects of the object.

For example, you can modify the column headers of each column in a DataGrid object after you have bound the object to data.

Changing the column header text in a DataGrid object


Additionally, you can use the AlternatingRowBackground property in a DataGrid object to specify the color of each alternating row.

For general information about modifying objects, see Working with solutions, projects, and files.

For specific information about the properties of individual objects, see the class library topics for Windows Presentation Foundation (starting at the System.Windows.Controls Namespace) and Microsoft Silverlight (starting at the System.Windows.Controls Namespace) on MSDN.

If the object is a control that uses templates, you can modify the templates to change the appearance of that object and all other objects that use the same templates.

Most controls use multiple templates for different pieces of the control. For example, the ListBox control uses many nested templates to style each piece of the ListBox: a scroll viewer, scroll bars, the thumbs of the scroll bars, and paging buttons.

The nesting of templates in the ListBox control


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.

The ListBox control also includes a data template that styles the items that are displayed in each row of the object. The data template is specific to the items that you bind to the object. For example, if you drag a string data item and an image data item from the Data panel onto a ListBox, the data template will contain a TextBlock to display the string and an Image to display the image file. You can style these objects in the data template.

You can make a control look very different from its default appearance. For example, the default appearance of a ListBox object looks something like the following:

A ListBox object using the default template


However, by modifying templates, you can make a ListBox object look like the following (from the ColorSwatchSL sample that is included with Blend for Visual Studio 2012):

The ListBox object using two modified templates (ItemContainerStyle and ItemsPanel)


To access the templates that are applied to objects, right-click the object or select the object and use the breadcrumb bar at the top of the artboard.

Opening a template by using the breadcrumb bar


Within a template, you can add, remove, and modify objects the same way that you would outside a template. The functionality of the ListBox control remains the same: it contains a list of items, of which one or many can be selected.

For specific examples of modifying data templates, see Use the DataGrid control and Use the TreeView control.

For more general information about styling controls, see Styling tips for WPF Simple Styles and Styling tips for common Silverlight controls.

For detailed information about the parts of a control or a template, see Styling a control that supports templates, or see the class library topics for Windows Presentation Foundation (starting at the System.Windows.Controls Namespace) and Silverlight (starting at the System.Windows.Controls Namespace) on MSDN.