Skip to main content

Data Presenter

Overview

DataTemplates are great for displaying items inside lists, but, what about displaying a single data object in your page? The DataPresenter control enables the scenario: present a single data object anywhere.

Let’s imagine you want to display a credit card bound to a Card property:

<grial:DataPresenter Data="{Binding Card}">
<grial:DataPresenter.DataTemplate>
<DataTemplate>
<!-- Card visualization XAML >
</DataTemplate>
</grial:DataPresenter.DataTemplate>
</grial:DataPresenter>

As simple as that. You can also define that DataTemplate within a resource dictionary. Then, you can use the DataPresenter to exhibit cards anywhere without the need to create a separate ContentView file just to store a piece of XAML with an obscure data binding contract.

Properties

NameTypeDescription
CacheRulesboolGets or sets wether this control should cache templates resolved through TemplatingRules.
DataobjectGets or sets the data object that will be presented through a data template.
DataTemplateDataTemplateGets or sets the data template used to present the data. If not set, the control will look for TemplatingRules to resolve the template.
TemplatingRulesCollection<TemplatingRule>Attached property used to define the templating rules in ancestors of the visual hierarchy.

Templating rules

There're other scenarios that require an extra level of dynamism. For instance, Grial's Registration Wizard Flow, provides multiple DataTemplates for the different wizard steps. In a scenario like that, it's handy to be able to define a set of DataTemplates along with the rules in which they should be selected. Once you define templates and rules, you can control the pages content from your ViewModels without going back to the XAML!

Let's take a look at how this works.

The DataPresenter defines an attached property TemplatingRules that holds a collection of rules. A DataPresenter with no Template property set, will traverse up the visual hierarchy looking tor a TemplatingRule collection to select the specific template. This mechanism is truly flexible and makes things very dynamic.

The main Wizard page defines a set of rules like this:

    <grial:DataPresenter.TemplatingRules>
<grial:TemplatingRule TargetType="local:StepEntryViewModel" TemplateType="local:WizardEntryItemTemplate" />
<grial:TemplatingRule TargetType="local:StepDatePickerViewModel" TemplateType="local:WizardDatePickerItemTemplate" />
...
</grial:DataPresenter.TemplatingRules>

And then, the main view model defines a specific list of step-view-models, each of which has a matching rule.

Properties

NameTypeDescription
TargetTypeTypeGets or sets the expected type of the data object this rule accepts.
TemplateDataTemplateGets or sets the template to use when this rule is applied. Use either this property or TemplateType property.
TemplateTypeTypeGets or sets the type of the visual element used to display the data. Use either this property or Template property.