I'm looking for a technique of how to customize text in DataGrid groups. For example, in my app, Iām grouping items by the MD5 hash tag, and it's do this with now problems. Except the format is quite unfriendly as instead of understandable group name I have MD5 hash. So, I would like to set human readable name in the group head + add some extra metrics, but keep MD5 hash hidden. Does anyone know is this doable or not?
Asked
Active
Viewed 63 times
-1
-
so your question is if there is a agreed upon standard to make a md5 hash more beautiful? (aka easier to read for non-technical user) ā Rand Random Jul 28 '23 at 13:14
-
Nope Rand. The question is how to customize DataGrid Group Header? ā Max Zaikin Jul 28 '23 at 13:23
-
well the group header normally is just the .ToString() of what ever object you have passed as an item - so just override ToString - https://dotnetfiddle.net/PIQvG9 ā Rand Random Jul 28 '23 at 13:26
1 Answers
1
I think I was able to figure it out my self with great help of Google and XAML Brewer. The solution is follwoing:
Make sure you have added
LoadingRowGroup
event to your XAML<controls:DataGrid Grid.Row="3" Name="FilesSearchDataGrid" AutoGenerateColumns="False" CanUserSortColumns="True" Sorting="DataGrid_Sorting" **LoadingRowGroup="DataGrid_LoadingRowGroup"** ItemsSource="{x:Bind ViewModel.Source, Mode=OneWay}" RowGroupHeaderPropertyNameAlternative="FileName">
In code behind add follwoing handler where you can customize group header as you need
private void DataGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e) { long groupFileSize = 0; ICollectionViewGroup group = e.RowGroupHeader.CollectionViewGroup; oFileInfo item = group.GroupItems[0] as oFileInfo; var count = group.GroupItems.Count; foreach (oFileInfo groupItem in group.GroupItems) { groupFileSize += groupItem.lSize; } e.RowGroupHeader.PropertyValue = $"{item.FileName} - {GetBytesReadable(groupFileSize)}" ; if (_grouping == "MD5") { // } else { // } }
My result:
P.S.
I would like to appreciate mr. Diederik Krols for his amazing WinUI KB 'XAML Brewer' site. For those who are newbie like me, this resource going to be a super great helper in XAML and WinUI Techniques.
Dear mr. Krols Thank you so much for sharing your knowledge and experience.

Rand Random
- 7,300
- 10
- 40
- 88

Max Zaikin
- 65
- 9