Use the DataGrid control
The DataGrid control can display a collection of data in multiple columns and rows. It includes functionality to sort by columns.
For more information about the DataGrid control, see DataGrid Class (System.Windows.Controls) on MSDN.
Create sample data to display in a DataGrid control
You can use any type of data in the DataGrid control. If you don't already have data that you can use in the procedure "To populate a DataGrid control," later in this topic, follow this procedure to set up sample data that will fully illustrate the features of the DataGrid control.
To create sample data to display in a DataGrid control
If you can't see the Data panel, click Data on the the Window menu.
In the Data panel, click Create sample data , and then click New Sample Data.
In the New Sample Data dialog box, you can change the default name for the data set, and select the part of your project to which you want the data made available.
Select the Enable sample data when application is running box.
ヒント The Enable sample data when application is running box lets your project display the sample data when you run your application. When the box is not selected, Blend for Visual Studio 2012 will display the sample data only on the artboard.
For more information, see Make your application display sample data when it runs.
Click OK.
A sample data source that includes a collection of properties (string and Boolean ) is created.
To fully explore the features of sample data and the DataGrid control, add an image property to the collection.
Next to Collection , click Add simple property to add a new property to the collection.
ヒント You can click the drop-down arrow next to the Add simple property button to select different types of properties to add: simple, complex, and collection.
Next to the new property, click Change property type . In the editor that appears, click the drop-down arrow next to Type, and then click Image.
Click anywhere outside the editor to close it.
You now have a sample data collection ready to display in a DataGrid control.
For information about modifying sample data and adding more data properties, see Modify sample data.
Populate a DataGrid control
There are multiple ways to bind data to a control on the artboard. The following procedure shows you the fastest way when you are using a DataGrid control.
For other options, see Bind an object to data.
To populate a DataGrid control
In the Tools panel, click Assets .
ヒント Alternatively, you can display the Assets panel in a docked panel by clicking Assets on the Window menu.
In the Assets panel, expand Controls, click All, and then click the DataGrid control.
The button underneath Assets in the Tools panel displays the icon for the DataGrid control and is selected.
Using your mouse, draw a large DataGrid object on the artboard in your main document.
In the Data panel, drag your sample data collection ( Collection) onto the new data grid object.
The data grid displays the collection in the sample data source, converting image properties to image objects, and converting Boolean properties to check boxes.
Press F5 to run your application.
If you are using the sample data from the previous procedure, and you enabled the sample data when testing your application, you should see the following:
ヒント To update a DataGrid object on the artboard after modifying the sample data that it displays, you can drag individual data properties onto the control to add new columns, or you can drag the entire collection again. In the Objects and Timeline panel, you can expand the Columns node of the DataGrid object to delete and reorder the columns.
Change column labels in a DataGrid control
When you drag a collection of data onto a data grid control, the column labels are set by the names of the properties. However, you can change the column labels after you populate the data grid control.
To change column labels in a DataGrid control
In the Objects and Timeline panel, expand the [DataGrid] object to reveal the column objects.
Select the first column object ([DataGridTemplateColumn] "Property1"). In the Properties panel, next to the Header property, type a descriptive name, such as Description, and then press ENTER.
Select the other two column objects and change their names.
Press F5 to run your application.
If you are using the sample data from the previous procedure, and you enabled the sample data when testing your application, you should see something similar to the following:
Change the appearance of the DataGrid control
You can change the appearance of controls by modifying their properties or by modifying the templates that are used to style them. You can modify the overall template, an empty row template, and the templates applied to individual cells, header templates, and others.
For more information, see Create or modify a template.
To change the appearance of column headers
In the Objects and Timeline panel or on the artboard, right-click the DataGrid object, point to Edit Additional Templates, point to Edit ColumnHeaderStyle, and then click Edit a Copy.
In the Create Style Resource dialog box, click OK.
Blend enters template-editing mode. The breadcrumb bar at the top of the artboard shows that you are editing a template inside the DataGrid object.
In the Objects and Timeline panel, add other objects to the template, or modify the properties of existing objects in the Properties panel. For example, in the [Grid] object, there is a path named SortIcon that represents an icon for a sort button. You could change this to an image or modify the path. The sort button appears when you run your application and click a column header.
ヒント Some properties might show a yellow bounding box to indicate that they are bound to data or bound to properties of the control that uses the template (template binding).
For more information about template binding, see Styling objects.
To change the appearance of cells that contain images
On the artboard, right-click a cell in the data grid object that is displaying an image, point to Edit Column Templates, point to Edit CellTemplate, and then click Edit Current.
In the Objects and Timeline panel, add other objects to the [StackPanel] layout container (such as a copyright symbol), or select the [Image] object and modify its properties in the Properties panel.
The changes that you make will affect all the cells that contain images.
To change the colors of alternating rows
In the Objects and Timeline panel, select the [DataGrid] object.
In the Properties panel, under Rows, click inside the rectangle next to the AlternatingRowBackground property.
A brush editor appears.
Select a type of brush by clicking one of the tabs at the top of the color editor. If you use the existing brush (Solid color brush ) and change the color, make sure to increase the Alpha property so that the color is not transparent.
You can change the colors of the other rows by modifying the RowBackground property.
For more information about modifying brushes, see Setting colors, brushes, and masks.