This is a migrated thread and some comments may be shown as answers.

DataTemplate and HierarchyChildTemplate

2 Answers 152 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 04 Jun 2015, 12:50 PM

Hi,

I would like to create a Grid using Hierarchy programmatically

 

radGridView.ChildTableDefinitions.Add(new GridViewTableDefinition());
var g = new FrameworkElementFactory(typeof(TextBlock));
g.SetValue(TextBlock.TextProperty, "TEST");
DataTemplate dt = new DataTemplate();
dt.VisualTree = g;
radGridView.HierarchyChildTemplate = dt;

I can developp/reduce hierarchy but nothing in details. I did the same into xaml code with success.

How to code correctly datatemplate ?

Thanks 

Regards

Eric

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Ivanov
Telerik team
answered on 05 Jun 2015, 05:18 AM
Hello,

Generally, using FrameworkElementFactory is one of the ways to create DataTemplates at runtime. Alternatively, you can use XamlReader.Load() to create DataTemplates from strings, which might be the more powerful approach, but in my opinion it is more difficult to maintain. Both techniques should be equally viable for this scenario. 
As for the code snippet that you sent, it looks good to me. You only have to add dt.Seal() before using it to initialize the HierarchyChildTemplate. Can you please confirm whether this change fixed things up?

Regards,
Ivan Ivanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Eric
Top achievements
Rank 1
answered on 05 Jun 2015, 07:18 AM

Hi Ivan,

You're right, I added dt.Seal() and all work perfectly. I also tried to use xaml string with success. Here is the alternative code:

 

string dataTemplate = "<DataTemplate><TextBlock Text=\"TEST\"/></DataTemplate>";
var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[0]);
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
context.XmlnsDictionary.Add("telerik", "http://schemas.telerik.com/2008/xaml/presentation");
 
var template = (DataTemplate)XamlReader.Parse(dataTemplate, context);
radGridView.HierarchyChildTemplate = template;

Regards

Eric

MV SOLUTIONS

Tags
GridView
Asked by
Eric
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Eric
Top achievements
Rank 1
Share this question
or