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.
How to use the same DataTemplate and bind to outside dynamic properties
My question is this: is there any way I use just one DataTemplate and then modify it per column?
Short answer: No, at least not in XAML. You must define a template as a whole. I am afraid you can't "inherit" all parts but the binding.
You may consider creating the templates programmatically:
Is there a way to build a DataTemplate with only C#
Changing data binding in DataTemplateSelector
The LoadContent()
method creates an instance of the Image
element that you have defined in the template. You are then setting the properties of this specific instance and return the unmodified template that you have defined in the XAML. The Image element that you modify in the SelectTemplate
method will then be eligible for garbage collection immediately and will never be used.
So this approach won't work. You need to return a completely different DataTemplate
from the method. You can't base a DataTemplate
on another one. You might as well get rid of the DataTemplate
from your XAML and use the XamlReader.Parse to create a DataTemplate
dynamically in your DataTemplateSelector
. Check my answer here for an example:
Combining DataTemplates at runtime
Your current approach applies the template and throws away the result.
Is it better to use a bunch of view models/data templates to wrap generic types, or a large data template selector?
but I have not been able to get XAML to recognize a generic type.
You're right, it can't :(.
So I have come up with two options
These are the only two options you have basically. There is not much else to it than what you already discovered I am afraid.
Whether you prefer to define a DataTemplate
per "wrapper" type or use a DataTemplateSelector
is a matter of personal flavour really. There is no right and wrong.
But your approach - whichever you choose - is the correct one.
Which DataTemplate is selected based on DataType if multiple match
According to the source code of FrameworkElement.FindTemplateResourceInternal
, WPF will search resource for DataTemplate
by track back the data item's inheritance.
Related Topics
What Task Is Best Done in a Functional Programming Style
Closures Behaving Differently in for and Foreach Loops
Change Custom Attribute's Parameter at Runtime
How to Implement Permission Based Access Control with ASP.NET Core
How Better to Resolve Dependencies in Object Created by Factory
How to Dynamically Change Auto Complete Entries in a C# Combobox or Textbox
Execute Multiple Queries in Single Oracle Command in C#
Define a Generic That Implements the + Operator
C# Flickering Listview on Update
Why "Decimal" Is Not a Valid Attribute Parameter Type
Convert Datatable to Ienumerable<T>
How to Parse Dates with a Suffix "Th", "St" or "Nd" on the Day of the Month
Httpcontext.Current Is Null When Unit Test
How to Count the Number of Elements That Match a Condition with Linq
JSON Deserialization - Map Array Indices to Properties with JSON.Net