Create or modify a data template
A data template defines the structure and format in which you would like your data to be represented. For more information, see Data templates. For a complete example, see Creating an RSS news reader.
- On the artboard or under Objects and Timeline in the Interaction panel, click the object that you want to create a binding on—this is where the data will flow into from the data source.
- In the Properties panel, click the Advanced Property Options
button next to the property that you want to serve as the target for the
binding—this is the specific property that will hold the data. In the pop-up window, click Data Binding.
The Create Data Binding dialog box opens.
The property you select must support data templates. If it does not, the Define Data Template button will not be present in the Create Data Binding dialog box. Properties that represent lists of items (the ItemsSource property of items controls) and content (Content property) support data templates.
- Select one of the tabs at the top of the Create Data Binding dialog box, depending on the type of data binding that you want to create. For information about these tabs, see Data binding.
- Do one of the following to enable the Define
- If you selected the Data Field tab, select a data source and then select the field that you want. If there are no data sources, you need to create one by clicking one of the buttons below the Data sources window. For an example, see Add or remove a data source or Creating an RSS news reader.
- If you selected the Element Property tab, select an element from the list that is displayed on the left, and then select one of the properties of the element that is displayed on the right
- If you selected the Explicit Data Context tab, select a field. If no fields are displayed, you need to use the Data Field tab, or you need to define the DataContext property first, either on this object or on an object that is higher up in the object hierarchy. For more information, see Bind data to a property or element or Creating an RSS news reader.
- Click the Define
DataTemplate button near the bottom of the dialog box.
The Create Data Template dialog box opens.
- In the Create Data Template dialog box, you have the
- Default control Styling Use the default styling for the control. In the case of binding to a list of items, the default styling is most often a sequence of text boxes. For more information, see the Data Templating Overview on MSDN.
- Current or Predefined Data Template Select a current or predefined data template resource from the drop-down box. This option is disabled if no suitable data template resource already exists for the control.
- New Data Template and Display Fields This option allows you to choose which data fields are included in the template and to select the type of control that should be assigned to each data field. For example, if you have a String data field, the default control that is used to display the field is a TextBlock. You can click the drop-down arrow next to the TextBlock to select a different control to display the data field. You can also use this method to choose what type of container element is wrapped around all of the fields, by clicking the drop-down arrow next to the control that is listed for the top-most item. Use the arrow buttons to determine the order in which the data fields appear. If you do not have a live data binding, select the Generate sample data check box so that you can view your control with sample data on the artboard to determine whether you like the look of your template.
- Under Objects and Timeline in the Interaction panel, right-click the object,
point to Edit Other Templates, point to Edit
Generated Items, and then click Edit Template.
Expression Blend will enter template editing mode, and you can see your template
structure under Objects and Timeline.
If you try to modify an existing data template by clicking the Advanced Property Options button next to the property for which you already created a data template, clicking Data Binding, and then clicking the Define Data Template button, you will actually create a new data template.
- Double-click the root panel object to activate it so that you can add more child objects.
- From the Toolbox, select a control to add to the template. For example, if there is another String data element that you want to display, select a text control such as the TextBlock from the Toolbox, and then double-click its icon to add the control to your template.
- With the new control selected under Objects and Timeline,
locate the property in the Properties panel to which you want
to bind data. For a TextBlock, this would be the Text property.
When you add a text control to the artboard, Expression Blend enters the editing mode for the control. This means that you can immediately type content into the control, but you cannot access all of the properties for the control. To exit the editing mode for the control, press ESC or click the Selection tool.
- Click the Advanced Property Options
next to the property you have chosen, and click Data
The Create Data Binding dialog box opens.
- Select the data item to which you want to bind the property, and then click Finish.
- To exit the editing mode for the template, click the Scope Up button under Objects and Timeline.