Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the DataType property on a WPF DataTemplate?

Tags:

c#

.net

wpf

xaml

So obviously I am doing something wrong, but I just cannot seem to get the HierarchicalDataTemplate (or even just DataTemplate) to work when using the DataType property. I have created the shortest possible WPF application to demonstrate the problem.

XAML:

<Window x:Class="WpfApplication1.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:System="clr-namespace:System;assembly=mscorlib"     xmlns:local="clr-namespace:WpfApplication1"     Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">     <Window.Resources>         <HierarchicalDataTemplate DataType="x:Type local:Foo">             <TextBlock Text="I am a Foo" />         </HierarchicalDataTemplate>         <HierarchicalDataTemplate DataType="x:Type System:String">             <TextBlock Text="I am a String" />         </HierarchicalDataTemplate>     </Window.Resources>     <Grid>         <TreeView Name="treeView1" ItemsSource="{Binding}" />     </Grid> </Window> 

CODE:

namespace WpfApplication1 {     public class Foo     {         public string Name { get; set; }     }      public partial class Window1 : Window     {         public Window1()         {             InitializeComponent();         }          private void Window_Loaded(object sender, RoutedEventArgs e)         {             var list = new List<object> { "a", 1, "b", 2, new Foo() { Name="Brian"}};             treeView1.DataContext = list;         }     } } 

Obviously I am expecting it display the following in the treeview.

I am a string 1 I am a string 2 I am a foo 

But my application actually displays the following.

a 1 b 2 WpfApplication1.Foo 

The strange thing is that almost every example I see on the web does this very thing (with slight variations) and no one else seems to be having a problem with it. Yet I have tried countless different ways of rearranging the XAML and nothing seems to help. I am hoping another pair eyes can help.

like image 261
Brian Gideon Avatar asked Sep 15 '09 00:09

Brian Gideon


People also ask

What is DataTemplate WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.

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).

Where do you put DataTemplate?

According to Microsofts App Studio the DataTemplates should live in a DataTemplates Subdirectory under the Views Directory.

Where is DataTemplate control in WPF?

ListBoxItemPhaseListBoxObj = (ListBoxItem)(PhaseListBox… ContainerFromIndex(0)); ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(PhaseListBoxObj);


1 Answers

Your code is fine, but your DataType attribute values need to be wrapped in curly braces:

<HierarchicalDataTemplate DataType="{x:Type local:Foo}">     <TextBlock Text="I am a Foo" /> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type System:String}">     <TextBlock Text="I am a String" /> </HierarchicalDataTemplate> 
like image 141
Matt Hamilton Avatar answered Oct 02 '22 07:10

Matt Hamilton