Creating a DataTemplate in Code: Can I use the Template property?
I'm attempting to use a DataTemplateSelector
with a particular third-party WPF grid control, and I'm having trouble determining if the issues I'm having are a bug in the control or my own lack of understanding about the conventions of WPF data templates.
I realize that the ordinary use case of a DataTemplate
is to declare it somewhere in XAML (be it as a resource or explicitly where it's used), but my particular project would benefit greatly if I could create the template in code (C#, specifically) rather than in XAML. The issue I'm running into is the fact that my code-created Da开发者_如何学编程taTemplate
uses a FrameworkElementFactory
as the template's VisualTree
, whereas a XAML-created template uses a TemplateContent
object as the template's Template
value. As best I can tell right now, the grid control in question works with templates that use Template
, but doesn't seem to play nicely with templates that use VisualTree
.
By way of comparison, here's what one of the templates looks like in XAML as part of my selector:
<MySelectorType>
<MySelectorType.BooleanTemplate>
<DataTemplate>
<EditorControl Name="Reserved_Name" />
</DataTemplate>
</MySelectorType.BooleanTemplate>
</MySelectorType>
And here's how I'm trying to create an equivalent template in code:
var template = new DataTemplate()
{
VisualTree = new FrameworkElementFactory(typeof(EditorControl))
{
Name = "Reserved_Name"
}
};
I've also tried it like this:
var template = new DataTemplate()
{
VisualTree = new FrameworkElementFactory(typeof(EditorControl))
};
template.VisualTree.SetValue(EditorControl.NameProperty, "Reserved_Name");
Which seemed more analogous to what the XAML template would do, but that appeared not to work at all (the editor neither read or set the value, where at least the first version would read it).
Is it possible for my in-code template to use the Template
property rather than VisualTree
? According to the documentation, there's no public API for this type and the instantiation path is complex, but has this been done? The only example I've found uses hardcoded XAML in the code, which doesn't sit well with me.
I do not like this way of doing things either but this actually is the recommended way, in the documentation of the FrameworkElementFactory
the following can be found:
This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.
I do not know of any simple way to use the Template
property in code, the only way to my knowledge that might be possible is via a lot of reflection.
Setting names is a special case, if you set the Name
property of the factory it should be properly registered, if not you need to get the approriate Namescope
and register the name manually.
精彩评论