Partilhar via


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.

Modify the properties of the object in the Properties panel

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

JJ170937.a2cebe0b-dc3d-45d5-9a30-23639e4bef48(pt-br,VS.110).png

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.

Modify one of the templates that are applied to the object

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

JJ170937.b8a37dc0-a65e-4fe1-8a9f-df485bb1bbae(pt-br,VS.110).png

JJ170937.095c13e8-6ed8-4572-9bd2-b2d6bdfdf199(pt-br,VS.110).png

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.

JJ170937.e1a62fcf-2f5c-4263-95fe-a9bf97616b85(pt-br,VS.110).png

A ScrollViewer control inside the ListBox template.

JJ170937.b36987f6-e914-4b58-99f2-a76c47128837(pt-br,VS.110).png

An unbound item in the ListBox control.

JJ170937.41999197-e26c-42b9-8039-58ce07f5b5ba(pt-br,VS.110).png

A vertical ScrollBar control inside the ScrollViewer template.

JJ170937.a5d608f2-bba2-48c5-8b15-2c115db86acc(pt-br,VS.110).png

A vertical Thumb control inside the ScrollBar template.

JJ170937.15de085f-48f5-41dd-a286-e3dcb4cfd18b(pt-br,VS.110).png

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

JJ170937.1d2fe38a-3fd8-4951-b978-7e44b6f2401d(pt-br,VS.110).png

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

JJ170937.f3190de1-098e-4dc0-bc1d-4a44d091685f(pt-br,VS.110).png

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)

JJ170937.0a581ba0-08e1-4116-91bf-d1309c645ae0(pt-br,VS.110).png

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

JJ170937.a7257528-6a90-47ac-8c31-ad3422395f38(pt-br,VS.110).png

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.

Consulte também

Conceitos

ListBox control styling tips

SimpleListBox and SimpleListBoxItem