I have a ListBox that presents a databound list of objects via its ItemSource. Because each object has special display needs I’m defining an ItemTemplateSelector that return
You could create a new CustomControl
that fits your needs. It will apply the style by itself and you can give additional DepdendencyProperties
to make it more convinient. In the end you can still put it in a DataTemplate
to use it with your DataTemplateSelector
.
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.