Combining DataTemplates at runtime

You could create the DataTemplate dynamically using the XamlReader.Parse or XamlReader.Load method, e.g.: string template = “<DataTemplate xmlns =\”http://schemas.microsoft.com/winfx/2006/xaml/presentation\” xmlns:x =\”http://schemas.microsoft.com/winfx/2006/xaml\”><StackPanel>[PLACEHOLDER]</StackPanel></DataTemplate>”.Replace(“[PLACEHOLDER]”, “…custom code…”); return System.Windows.Markup.XamlReader.Parse(template) as DataTemplate; The custom parts could be defined as UserControls. I am afraid there is no way to base a DataTemplate on another one in pure XAML though.

Change Data template dynamically

A DataTemplateSelector does not respond to PropertyChange notifications, so it doesn’t get re-evaluated when your properties change. The alternative I use is DataTriggers that changes the Template based on a property. For example, this will draw all TaskModel objects using a ContentControl, and the ContentControl.Template is based on the TaskStatus property of the TaskModel <DataTemplate … Read more