-1

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?

This is what my app shows right now: enter image description here

This is what i would like to have instead enter image description here

Max Zaikin
  • 65
  • 9
  • 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 Answers1

1

I think I was able to figure it out my self with great help of Google and XAML Brewer. The solution is follwoing:

  1. 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">
    
  2. 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:

enter image description here

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