How To Use GroupHeaderTemplateSelector

3 posts, 1 answers
  1. Cody
    Cody avatar
    3 posts
    Member since:
    Sep 2017

    Posted 28 Sep Link to this post

    So I have a datagrid with a column based off a property called 'Category'. Now depending on the value of that property I want the DataTemplate for the GroupHeader to be different. I am unsure of how to obtain the value of 'Category' for each group. I was unable to find any examples of how to use GroupHeaderTemplateSelector however I have this example of how to use CellContentTemplateSelector below. In this example the method takes a parameter 'object item'. In the context of a cell selector it is apparent that this item is the Object from the ItemsSource list that the grid is being generated from. A group is a collection of objects from that list though so I am unsure what the equivalent method for GroupHeaderTemplateSelector would return as it's 'object item' so that I can access the 'Category' property that the group is being built from. Any help is appreciated!
     
    protected override Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container)
        {
            if ((item as CustomObject).Flag == null)
            {
                return this.NoImageFoundTemplate;
            }
            else
            {
                return this.ImageTemplate;
            }
        }
  2. Answer
    Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    503 posts

    Posted 02 Oct Link to this post

    Hi, Cody,

    Please, have a look at the following article - RadDataGrid: Grouping Styling. At the end of the article, there is an example that shows how to use the GroupHeaderTemplateSelector. As shown there, the item should be cast as GroupHeaderContext. Eventually, you can check what properties you can work with according to the scenario you would like to achieve. Here is an example:

    protected override DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container)
           {
               var groupHeaderContext = item as GroupHeaderContext;
     
               if (groupHeaderContext != null)
               {
                   if ((item as GroupHeaderContext).Group.Key.ToString().Equals("Brazil"))
                   {
                       return this.ExpandedTemplate;
                   }
                   else
                   {
                       return this.CollapsedTemplate;
                   }
               }
               return this.CollapsedTemplate;
           }


    Regards,
    Stefan Nenchev
    Progress 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. Cody
    Cody avatar
    3 posts
    Member since:
    Sep 2017

    Posted 02 Oct in reply to Stefan Nenchev Link to this post

    This is perfect! Thank for the response!
Back to Top