Export (0) Print
Expand All

Style a control that displays data

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

a2cebe0b-dc3d-45d5-9a30-23639e4bef48

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

b8a37dc0-a65e-4fe1-8a9f-df485bb1bbae
095c13e8-6ed8-4572-9bd2-b2d6bdfdf199

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.

e1a62fcf-2f5c-4263-95fe-a9bf97616b85

A ScrollViewer control inside the ListBox template.

b36987f6-e914-4b58-99f2-a76c47128837

An unbound item in the ListBox control.

41999197-e26c-42b9-8039-58ce07f5b5ba

A vertical ScrollBar control inside the ScrollViewer template.

B1_5

A vertical Thumb control inside the ScrollBar template.

B1_6

A vertical RepeatButton control inside the ScrollBar template to decrease the value of the ScrollBar control in large increments.

B1_7

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

f3190de1-098e-4dc0-bc1d-4a44d091685f

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):

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

0a581ba0-08e1-4116-91bf-d1309c645ae0

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

a7257528-6a90-47ac-8c31-ad3422395f38

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.

Show:
© 2014 Microsoft