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

How To Use GroupHeaderTemplateSelector

2 Answers 39 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Cody
Top achievements
Rank 1
Cody asked on 28 Sep 2017, 05:12 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Stefan Nenchev
Telerik team
answered on 02 Oct 2017, 03:17 PM
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
0
Cody
Top achievements
Rank 1
answered on 02 Oct 2017, 06:05 PM
This is perfect! Thank for the response!
Tags
DataGrid
Asked by
Cody
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Cody
Top achievements
Rank 1
Share this question
or