New Line of Business Features in Silverlight 5 – Implicit Data Templates

Silverlight 5 has a a lot of new features that are attractive to marketing and media application developers as well as Line of Business (LOB) developers. My company focuses on building LOB applications for customers so some of the new features related to that area of development really stood out to me. Over the next few weeks I’ll blog about a few of my personal favorites including:

  • Implicit Data Templates
  • Ancestor RelativeSource Binding
  • Debugging Bindings in XAML
  • Elevated Trust Enhancements
    • Using pInvoke
    • Running with Elevated Trust In-Browser
    • Creating Multiple Windows in Out-of-Browser Applications
  • Vector Printing
  • Custom Markup Extensions

There are of course a lot more features than I’ve listed above, but they’ll provide a nice starting point. In this first post I’ll jump right into one of my favorite new features – implicit data template support.

Using Implicit Data Templates

Data templates are an important part of Silverlight LOB applications that allow data to be formatted and displayed in a variety of ways. Prior to Silverlight 5, developers had to use keys defined in styles or templates to apply data templates to controls. While this approach worked fine, having the ability to apply templates implicitly based on bound types has been a popular feature request especially since that functionality is part of Windows Presentation Foundation (WPF).

Implicit data templates allow a type such as Person to be associated with a template. As a control binds to a collection of Person objects the data template will implicitly be used by the control allowing you to avoid explicitly defining data template keys. So how does it work? A new DataType property has been added to the DataTemplate object which is used to define the specific type the template is associated with. An example of using the DataType property is shown in Listing 1.

<UserControl
   xmlns:types="clr-namespace: MyTypesNamespace">

    <DataTemplate DataType="types:Person">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}" Margin="0,0,2,0" />
            <TextBlock Text="{Binding LastName}"  />
        </StackPanel>
    </DataTemplate>

    <ComboBox ItemsSource="{Binding People}" />
</UserControl>


Listing 1. Defining an implicit data template that will automatically be used when a Person type is bound to a control.

This example includes a ComboBox that is bound to collection property named People. No explicit data template is hooked to the ComboxBox. Instead, the data template is associated with a Person type and its FirstName and LastName properties are bound to TextBlock controls. As Person instances are bound to the ComboBox control, the data template is automatically applied and used to render the FirstName and LastName property values. This is done without explicitly defining a key on the data template as with Silverlight 4 and earlier.

What's great about this new functionality is that if a child type derives from Person and is bound to the ComboBox (such as a PremiumCustomer type), its implicit data template will automatically be used if available. If the PremiumCustomer type doesn't have a data template then the Person data template will be used. Listing 2 shows how multiple data templates can be defined for object hierarchies. In this example, PremiumCustomer types will be displayed with a yellow background to highlight data in the ComboBox (see the image below).


<DataTemplate DataType="types:Person">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding FirstName}" Margin="0,0,2,0" />
        <TextBlock Text="{Binding LastName}" />
    </StackPanel>
</DataTemplate>

<DataTemplate DataType="types:PremiumCustomer">
    <StackPanel Orientation="Horizontal" Background="Yellow">
        <TextBlock Text="{Binding FirstName}" Margin="0,0,2,0" />
        <TextBlock Text="{Binding LastName}" />
    </StackPanel>
</DataTemplate>


Listing 2. Defining a base data template for Person types as well as a more specialized data template for PremiumCustomer types. PremiumCustomer derives from Person.

clip_image001


Data templates are a great new feature that will really simplify the process of defining and using templates with Silverlight controls. I’m excited that they final made it into the Silverlight framework.

Published Thursday, October 06, 2011 4:58 PM by dwahlin
Filed under: , ,

Comments

# New Line of Business Features in Silverlight 5 ??? Implicit Data &#8230; | blog, iphone, app, creative, games, programming, project, various

Pingback from  New Line of Business Features in Silverlight 5 ??? Implicit Data &#8230; | blog, iphone, app, creative, games, programming, project, various

# New Line of Business Features in Silverlight 5 ??? Implicit Data Templates &#8211; Dan Wahlin&#8217;s WebLog &laquo; Hornet Dear Bernard

Pingback from  New Line of Business Features in Silverlight 5 ??? Implicit Data Templates &#8211; Dan Wahlin&#8217;s WebLog &laquo;  Hornet Dear Bernard

# Dew Drop &ndash; October 7, 2011 | Alvin Ashcraft&#039;s Morning Dew

Pingback from  Dew Drop &ndash; October 7, 2011 | Alvin Ashcraft&#039;s Morning Dew

# New Line of Business Features in Silverlight 5 ??? Implicit Data Templates

Pingback from  New Line of Business Features in Silverlight 5 ??? Implicit Data Templates

# New Line of Business Features in Silverlight 5–Ancestor RelativeSource Binding

Friday, October 07, 2011 1:22 PM by Dan Wahlin's WebLog

This is the second post in a series covering new line of business features in Silverlight 5. Implicit

# re: New Line of Business Features in Silverlight 5 – Implicit Data Templates

Sunday, October 09, 2011 10:18 AM by Nilesh Gule

Nice post Dan. I had a similar post related to Implicit Data Templates nileshgule.blogspot.com/.../ngtweet-part-16implicit-data-templates.html

# New Line of Business Features in Silverlight 5–Debugging Bindings in XAML

Saturday, October 15, 2011 11:25 AM by Dan Wahlin's WebLog

This is the third post in a series covering new line of business features in Silverlight 5. Implicit

# New Line of Business Features in Silverlight 5???Debugging Bindings in XAML

Pingback from  New Line of Business Features in Silverlight 5???Debugging Bindings in XAML

# New Line of Business Features in Silverlight 5 – Using pInvoke

Sunday, October 23, 2011 2:01 PM by Dan Wahlin's WebLog

This is the fourth post in a series covering new line of business features in Silverlight 5. Implicit

# New Line of Business Features in Silverlight 5 ??? Using pInvoke

Pingback from  New Line of Business Features in Silverlight 5 ??? Using pInvoke

# Silverlight 5 Released with a lot of Great New Features

Friday, December 09, 2011 4:04 PM by Dan Wahlin's WebLog

Silverlight 5 was released today and there are a ton of great new features available in this release