Creating a DataTemplate in code: can I use the Template property?

I am trying to use a DataTemplateSelector with a specific third-party WPF grid control, and I am having problems determining whether the problems I am having are a control error or my own lack of understanding of the WPF data pattern convention.

I understand that a common example of using a DataTemplate is to declare it somewhere in XAML (whether as a resource or explicitly where it was used), but my specific project will be of great benefit if I can create a template in code (C # in particular), not in XAML. The problem I ran into is that my code-generated DataTemplate uses FrameworkElementFactory as a VisualTree template, while a template created by XAML uses a TemplateContent object as the value of the Template template. As far as I can tell now, the grid control in question works with templates using Template , but does not seem to play well with templates using VisualTree .

As a comparison, here's which of the patterns in XAML looks like part of my selector:

 <MySelectorType> <MySelectorType.BooleanTemplate> <DataTemplate> <EditorControl Name="Reserved_Name" /> </DataTemplate> </MySelectorType.BooleanTemplate> </MySelectorType> 

And this is how I try to create an equivalent template in the code:

 var template = new DataTemplate() { VisualTree = new FrameworkElementFactory(typeof(EditorControl)) { Name = "Reserved_Name" } }; 

I also tried this as follows:

 var template = new DataTemplate() { VisualTree = new FrameworkElementFactory(typeof(EditorControl)) }; template.VisualTree.SetValue(EditorControl.NameProperty, "Reserved_Name"); 

Which seemed more like what the XAML template would have done, but it turned out that it doesn’t work at all (the editor does not read and does not set the value, where at least the first version will read it).

Is it possible for my intra- VisualTree template to use the Template property, not VisualTree ? According to the documentation , there is no public API for this type, and the instance creation path is complicated, but is this done? The only example I found uses hardcoded XAML code in code that doesn't work for me.

+4
source share
1 answer

I also don’t like this way of doing it, but it is really recommended, in the FrameworkElementFactory documentation the following can be found:

This class is an obsolete way to programmatically create templates that are subclasses of FrameworkTemplate, such as ControlTemplate or DataTemplate; Not all template functionality is available when creating a template using this class. The recommended way to programmatically create a template is to load XAML from a string or memory stream using the Load method of the XamlReader class.

I don’t know of any simple way to use the Template property in code, the only way to find out what might be possible is a lot of thought.


Settings names are a special case, if you set the Name property to factory, it must be correctly registered if you do not need to get apoporiate Namescope and register the name manually.

+2
source

All Articles