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

Custom Group Headers

6 Answers 309 Views
GridView
This is a migrated thread and some comments may be shown as answers.
haagel
Top achievements
Rank 1
haagel asked on 27 Aug 2010, 09:44 AM
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.

6 Answers, 1 is accepted

Sort by
0
haagel
Top achievements
Rank 1
answered on 27 Aug 2010, 09:51 AM
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...
0
Accepted
Yavor Georgiev
Telerik team
answered on 27 Aug 2010, 10:16 AM
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
0
haagel
Top achievements
Rank 1
answered on 27 Aug 2010, 10:59 AM
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?
0
Accepted
Yavor Georgiev
Telerik team
answered on 27 Aug 2010, 11:04 AM
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
0
Phil
Top achievements
Rank 1
answered on 08 May 2018, 11:40 PM

I handled the general need to display a custom group header like this. - the case is what is displayed when the group is collapsed in a RadGridView and solving the problem with the GroupHeaderTemplate.

The xaml is like this

<te:RadGridView.GroupHeaderTemplate><br>    <DataTemplate><br>        <!-- I couldnt find a Path binding that wouldnt break the converter,<br>             so I grab the default stuff the framework sends it.       --><br>        <TextBlock Text="{Binding Converter={StaticResource PropertyNameConverter}}" /><br>    </DataTemplate><br></te:RadGridView.GroupHeaderTemplate>

 

In the UserControl.Resources I need to put a key on my value converter

<UserControl.Resources><br>     <cons:PropertyNameConverter x:Key="PropertyNameConverter" ></cons:PropertyNameConverter><br> </UserControl.Resources>

 

and in the xmlns declarations a reference to the file the value converter is in

xmlns:cons="clr-namespace:SmartObjMgr.Helpers"

 

and the value converter looks like this.  I couldn't get Group.Key or Path to send a value to the value converter, but I found that the data value I needed was passed in the value parameter automatically.

public class PropertyNameConverter : IValueConverter<br>{<br>    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)<br>    {<br>        var IoValue = ((GroupViewModel)value).Header;<br>        string TheLabel = "";<br>        switch (IoValue)<br>        {<br>            case "Input":<br>                TheLabel = "Input Property";<br>                break;<br>            case "Return":<br>                TheLabel = "Return Property";<br>                break;<br>            case "Parameter":<br>                TheLabel = "Parameter Property";<br>                break;<br>            default:<br>                TheLabel = "Property";<br>                break;<br>        }<br>        if (TheLabel == "Property") { MessageBox.Show(IoValue + " needs to be handled in Helper, PropertyNameConverter.", "Edit App Code"); }<br>        return TheLabel;<br>    }

 

It works.

0
Dilyan Traykov
Telerik team
answered on 11 May 2018, 03:55 PM
Hello Phil,

I'm glad to hear that you've found a suitable solution. If you require any further assistance, please let us know.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
haagel
Top achievements
Rank 1
Answers by
haagel
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Phil
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or