Custom Group Headers

5 posts, 2 answers
  1. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 27 Aug 2010 Link to this post

    I want to change the default headers of the groups in the grid. There are two scenarios where I feel the need to do this.

    1. Complex data types
    When the value to group by is a simple datatype, say a string, the default headers work fine. But when grouping by a columns that is bound to a complex datatype it doesn't look as good. In those cases the headers will show all the properties of the class and the values in it.
    An example:
    Let's say I have a class:
    public class MemberType
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }
    My grid has one column that is bound to this type. In the column XAML I specify that the Description should be displayed and all works fine. But when I group by that column, the headers of all groups will not show only the Description but all properties, like this:
    "Id:1 Desciption:Editor" and so on. How can I specify that I only want to show the value of the Description i the group headers (which is what the rows are grouped by).

    2. Boolean values
    When the value to group by is a boolean, there will be two groups with the headers "true" and "false" (assuming both values are represented in the data). This is the desired behavior, but I would like the headers to be "Yes" and "No" instead.
  2. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 27 Aug 2010 Link to this post

    I realized there is an easy solution to the first scenario (complex type), and that is to implement a ToString() method on the type (and thus override the default one). That is what will be shown in the group header.

    However, it would be nice if there was another way. The ToString() method might already be used for other purposes...
  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Yavor Georgiev
    Admin
    Yavor Georgiev avatar
    982 posts

    Posted 27 Aug 2010 Link to this post

    Hello David Haglund,

     You can achieve the same result using the GroupHeaderTemplate property of GridViewColumn.

    Best wishes,
    Yavor Georgiev
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  5. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 27 Aug 2010 Link to this post

    But how?

    I found this example:
    <telerik:GridViewDataColumn.GroupHeaderTemplate>
        <DataTemplate>
            <TextBlock Foreground="Green" Text="{Binding Group.Key}" />
        </DataTemplate>
    </telerik:GridViewDataColumn.GroupHeaderTemplate>
    This will not change the text displayed, but only change the color of the text to green.

    I don't understand how I can use this to fix my scenarios. Could you give me an example?
  6. Answer
    Yavor Georgiev
    Admin
    Yavor Georgiev avatar
    982 posts

    Posted 27 Aug 2010 Link to this post

    Hello David Haglund,

     The Group.Key property contains your business object. Thus you can bind to its various properties such as Group.Key.MyProperty or Group.Key.MyOtherProperty.

    All the best,
    Yavor Georgiev
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready