Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my typed data template not being applied?

I'm using Linq To Sql to fill up a listbox with Segment objects, where Segment is designer created/ORM generated class.

<Window x:Class="ICTemplates.Window1"
    ...
    xmlns:local="clr-namespace:ICTemplates"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
      <DataTemplate x:Key="MyTemplate"> 
      <!--  <DataTemplate DataType="x:Type local:Segment"> -->
        // some stuff in here
      </DataTemplate>
    </Window.Resources>
    <ListView x:Name="tvwSegments" ItemsSource="{Binding}" ItemTemplate="{StaticResource MyTemplate}" MaxHeight="200"/>


// code-behind
var queryResults = from segment in tblSegments
                               where segment.id <= iTemplateSid
                               select segment;
tvwSegments.DataContext = queryResults;

This works.

However if I used a Typed Data Template (by replacing the x:Key with the DataType attribute on the template, the items all show up with ICTemplates.Segment (the ToString() return value)
The concept is that it should pick up the data template automatically if the Type matches. Can someone spot the mistake here?

like image 649
Gishu Avatar asked Dec 16 '08 09:12

Gishu


People also ask

What is difference between ItemTemplate and DataTemplate?

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.

Why to use data template?

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. In that case, you can use a DataTemplate to define the appearance of your data objects.

What is difference between a ControlTemplate and a DataTemplate in WPF?

A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).

What is a data template?

The data template is the method by which you communicate your request for data to the data engine. It is an XML document whose elements collectively define how the data engine will process the template to generate the XML. The data engine supports the following functionality: Single and multiple data queries.


1 Answers

Ze Mistake is here

<DataTemplate DataType="x:Type local:Segment">  <!-- doesn't work -->

should be

<DataTemplate DataType="{x:Type local:Segment}">

Came home... made a toy-sample and it worked with this change. Gotta try that @ work tomorrow. Sheesh.. for want of 2 curlies..

Update: Found out another gotcha

<DataTemplate x:Key="SegTemplate" DataType="{x:Type local:Segment}">  <!-- doesn't work -->

won't work. Seems like you can have it either with a Key OR DataType attribute. To make this typed data template work.. had to remove the Key attribute and now it works as expected. Behavior is consistent for a HierarchicalDataTemplate too.

<DataTemplate DataType="{x:Type local:Segment}">
like image 113
Gishu Avatar answered Sep 28 '22 04:09

Gishu