index > Windows Presentation Foundation ("Avalon") > DataTemplate vs. ControlTemplate

DataTemplate vs. ControlTemplate

Hello,

I'm not so sure that I understand the difference between a DataTemplate and ControlTemplate.

I realize that the DataTemplate renders a 'type' of data. But then why is ColumnHeaderTemplate defined using a DataTemplate resource?

Thanks

Houman

Houman b

They're mostly the same, getting most of their behavior from the base FrameworkTemplate class. The primary difference is that ControlTemplate has a TargetType property and DataTemplate has a DataType property.

Another difference is that the DataTemplateSelector can be used to choose a DataTemplate at runtime. So, for example, the ColumnHeaderTemplate that you mentioned has a companion ColumnHeaderTemplateSelector property.

Mike Hillberg - MSFT

Thanks Mike,

I'm still trying to figure out when to use what...but it is getting easier...

Houman

Houman b

Wow I've never thought about it this way. But plain and simple, whenever you want a control template to be assigned dynamically, you need DataTemplates along with a DataTemplateSelector.

For example, my grid control uses a datatemplate selector that looks at the type of cell (Text, Image, DropDown, CheckBox) and assigns the appropriate DataTemplate.

In cases where the control will never change, just use a controltemplate and be done with it




What's in your toolbelt? http://www.codeplex.com/Wiki/View.aspx?ProjectName=wpftoolbelt
ivolved_Mike_Brown

Thanks Mike, that makes sense.

Is there a good example out there on dataTemplateSelector sounds like a very powerful feature.

Thanks

Houman

Houman b

here is one from the samples

http://windowssdk.msdn.microsoft.com/en-us/library/ms771489.aspx

lee d
Thanks Lee
Houman b
reply 7

You can use google to search for other answers

 

More Articles

• Dynamic diagram creation
• how to access visuals created by streamGeometry in visualCollecti...
• MouseMove event handler delay
• Does "System.Windows.Controls.Label" support text wrapp...
• GroupStyle and WrapPanel
• Custom Window
• Styling expanders with HiearchicalDataTemplates
• Using LocBaml.exe Tool
• Changing default editor for XAML
• How to turn off anti aliasing for small text?
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Relative Image Path
• How to get the positon of the control's
• Add button with dropdown include menu wi
• When to use windows browser app?
• OnMediaEnded and weird MediaElement.Pla
• AutoScrolling Text in a ListBox?? Help P
• PasswordBox Databind or TextBox mask
• winfx wizard framework
• Healthcare application demo
• CompoundFile - Why oh why oh why -
• color space?
• How to Bind to an ADO.NET Data Source.
• how to keep doc file in document viewer
• How does one implement a Validation temp
• possible 3D rendering problem

Hot Articles

• Set Focus / Selection to child item
• Extending a page function
• Data Binding - SetBinding destroys Valid
• DoubleClick on FrameworkElement
• Binding to frame's Content property
• WPF Program Managers
• Animation cpu usage
• Changer +/- Signs In TreeView Help
• Cannot set Name attribute value 'Tab1' o
• Outlook Bar like control in WPF
• BitmapImage is not raising the DownloadC
• Sliding elements
• Creating graphs using WPF
• ppt file can't show on server
• How to transfer(Upload) file to remote s

Recommend Articles

• Video playback frame rate and performance
• how to add text to a 3d shape?
• Applying different DataTemplate to the s
• Animating Speed Ratio
• Source code for the WPF Tablet PC Demo
• databinding bug, or am i just expecting
• Whoops! (I'm getting tired of it)
• Dockable windows tool for WPF
• Bug ? The Enabled control is not effecti
• Multi-Monitor... no hardware drawing sup
• How to host a WBA on IIS 7.0 in Windows
• xbap's can't upload images??
• Bug when MouseLeave Event in ControlTemp
• How To Get Element within a Listview Cel
• IsItemsHost triggerable?