Skip to content

Latest commit

 

History

History
67 lines (42 loc) · 8.7 KB

datatemplate.md

File metadata and controls

67 lines (42 loc) · 8.7 KB
-api-id -api-type
T:Microsoft.UI.Xaml.DataTemplate
winrt class

Microsoft.UI.Xaml.DataTemplate

-description

Describes the visual structure of a data object. Use data binding for specific elements in the template that display the data values.

-xaml-syntax

<DataTemplate ...>
  templateContent
</DataTemplate>

-remarks

A DataTemplate object is used as the value for these properties:

You typically use a DataTemplate to specify the visual representation of your data. DataTemplate objects are particularly useful when you are binding an ItemsControl such as a ListBox to an entire collection. Without specific instructions, a ListBox displays the string representation of the objects in a collection. Use a DataTemplate to define the appearance of each of your data objects. The content of your DataTemplate becomes the visual structure of your data objects.

You typically use data binding in a DataTemplate. For example, suppose that a ListView is bound to a collection of Customer objects and has the ItemTemplate property set to a DataTemplate. When the ListView is created, a ListViewItem is created for each Customer in the collection, and the DataContext of the ListViewItem is set to the appropriate customer. In other words, the DataContext of the first ListViewItem is set to the first customer, the DataContext of the second ListViewItem is set to the second customer, and so on. You can bind elements in the DataTemplate to show property values that come from each of the Customer objects.

You can also use a DataTemplate to share UIElement objects across multiple ContentControl objects. For example, suppose you need multiple buttons on your application to have the same graphic. You can create a DataTemplate that contains the graphic and use it as the ContentTemplate for the buttons. A data template for ContentTemplate can also use data binding. But in this case the data context is the same as the element where the template's applied. Usually this is one data object, and there's no concept of items.

You can place a DataTemplate as the direct child of an ItemTemplate property element in XAML. This is know as an inline template and you'd do this if you had no need to use that same data template for other areas of your UI. You can also define a DataTemplate as a resource and then reference the resource as the value of the ItemTemplate property. Once it's a resource, you can use the same template for multiple UI elements that need a data template. If you factor the data template into Application.Resources, you can even share the same template for different pages of your UI.

The XAML usage for contents of a data template is not exposed as a settable code property. It is special behavior built into the XAML processing for a DataTemplate.

For advanced data binding scenarios, you might want to have properties of the data determine which template should produce their UI representations. For this scenario, you can use a DataTemplateSelector and set properties such as ItemTemplateSelector to assign it to a data view. A DataTemplateSelector is a logic class you write yourself, which has a method that returns exactly one DataTemplate to the binding engine based on your own logic interacting with your data. For more info, see Data binding in depth.

XAML attached properties

DataTemplate is the host service class for a XAML attached property.

In order to support XAML processor access to the attached properties, and also to expose equivalent get and set operations to code, each XAML attached property has a pair of Get and Set accessor methods. Another way to get or set the value in code is to use the dependency property system, calling either GetValue or SetValue and passing the identifier field as the dependency property identifier.

Attached property Description
ExtensionInstance Gets or sets an extension instance that defines helper methods for phased rendering of a data template.

-examples

The following example uses a DataTemplate to display the items of a ListView. In this example, the ListView is bound to a collection of Customer objects. The DataTemplate contains TextBlock controls that bind to the FirstName, LastName, and Address properties. For more info on data binding, see Data binding in depth.

[!code-xamlSnippet1_XAML]

[!code-csharpSnippet1_CS]

-see-also

FrameworkTemplate, ItemsControl.ItemTemplate, ContentControl.ContentTemplate, DataTemplateSelector, Data binding in depth, ResourceDictionary and XAML resource references