A First Look at Building Data Templates with Blend

   Posted by: Andrew Troelsen

In my previous blog entry, I examined how the Blend IDE Data panel allows you to bind collections of custom objects to UI elements. Recall the previous PurchaseOrder and PurchaseOrders classes:

   1: public class PurchaseOrder
   2: {
   3:   public PurchaseOrder(){}
   4:   public PurchaseOrder(int amt, double cost, string desc)
   5:   {
   6:     Amount = amt;
   7:     TotalCost = cost;
   8:     Description = desc;
   9:   }
  10:         
  11:   public int Amount { get; set; }
  12:   public double TotalCost{ get; set; }
  13:   public string Description{ get; set; }
  14: }
  15:  
  16: public class PurchaseOrders : 
  17:   System.Collections.ObjectModel.ObservableCollection<PurchaseOrder>
  18: {
  19:   public PurchaseOrders()
  20:   {
  21:     // Add a few items upon startup.
  22:     this.Add(new PurchaseOrder(5, 50.00, "Mikko's Cat Nip Treat"));
  23:     this.Add(new PurchaseOrder(5, 50.00, "Saku's Best Dog Bone"));
  24:     this.Add(new PurchaseOrder(1, 2.50, "Extra Bland Tofu"));
  25:   }
  26: }

In this post, we will use these classes to generate a new data template. As you may know, data templates are very similar to the control template model in that they can be used to define how to render out look and feel of a given blob of XAML.  As the name implies, a data template allows you to define the look and feel of a data binding operation.

If you were to create a new WPF project with Blend (and insert the previous class definitions into your project), you can then create a new Object Data Source connection to your PurchaseOrders object as you did in the previous blog post and drag the PurchaseOrders node from the Data panel onto the Window's artboard.

At this point, you should see the same display as shown below. Recall that the IDE generated a default data template which is used by the ListBox to display each property value in a simple, un-styled TextBlock.

Figure 6-19

The generated XAML looks like so:

   1: <DataTemplate x:Key="PurchaseOrderTemplate">
   2:   <StackPanel>
   3:     <TextBlock Text="{Binding Amount}"/>
   4:     <TextBlock Text="{Binding Description}"/>
   5:     <TextBlock Text="{Binding TotalCost}"/>
   6:   </StackPanel>
   7: </DataTemplate>

When you wish to modify this default data template, you will first need to open the Resources panel and locate the template by name. Once you have done so, open it for editing.

Figure 6-24

Now, locate the Objects and Timeline editor and select the first TextBlock.

Figure 6-25

At this point, you are free to use the Properties panel to change any settings you wish in order to style the display how the Amount property of each object will be displayed in the TextBlock. By way of a few suggestions, change the Foreground property to a new color value, and maybe tinker with the font settings using the Text area of the Properties window. Here is how I configured my first TextBlock.

   1: <TextBlock Text="{Binding Amount}" Foreground="#FFE91616" 
   2:            FontWeight="Bold" FontSize="18.667"/>

Now, if you run your program, you will see that each Amount property is looking somewhat more interesting (granted, it is not ready for primetime).

Figure 6-26

You can use this general approach to set the look and feel of each UI element which makes up the data binding template. Of course, it would be more interesting if we were to add some composite content, graphics or animations to the data template. We will do that in the next post!

See you then.


Comments (0)

Be the first one to comment on this post!

Add a Comment

*

*

Loading

Find Us
Contact Us 651-288-7000 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | Microsoft Web Development Training | Prism / MVVM / MEF Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2012 Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C / C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.