DataTemplate and HierarchyChildTemplate

3 posts, 1 answers
  1. Eric
    Eric avatar
    3 posts
    Member since:
    Feb 2009

    Posted 04 Jun 2015 Link to this post

    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. Answer
    Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1216 posts

    Posted 05 Jun 2015 Link to this post

    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
  3. Eric
    Eric avatar
    3 posts
    Member since:
    Feb 2009

    Posted 05 Jun 2015 Link to this post

    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

Back to Top