1
votes

Im very new to WPF and i am working with ListView and Grouping.

What im trying to achieve is to group items by certain type but display a different name on the ListView header. This will allow me to have 2 different group types with the same name

Heres my code so far

   //.....Populate dummy items in a loop
   items.Add(new Beds() { Name = "Bed " + counter, Type = i.ToString() ,  TypeName = "Test Name" });
   //....

   lvBeds.ItemsSource = items;

   CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvBeds.Items);
   PropertyGroupDescription groupDescription = new PropertyGroupDescription("Type");
   view.GroupDescriptions.Add(groupDescription);

XAML

   <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock FontWeight="Bold" FontSize="20" Text="{Binding TypeName}" Background="Gray" Height="35"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>

I want to map the ListView Header TextBlock Text to "TypeName" while keeping the grouping to "Type". Is that possible?

Edit: I solved this by using IValueConverter to handle the replacement of binding name string.

How to replace strings in StringFormat in WPF Binding

1
Awesome! So whats your problem?DerApe
I want to map the ListView Header TextBlock Text to "TypeName" while keeping the grouping to "Type". Is that possible?iamIcarus
Looks like you've set it up correctly. Is it not working?Joel Lucsy
@JoelLucsy unfortunately the header's text is emptyiamIcarus
Are you getting any binding errors in your output window when you run it in debug?Joel Lucsy

1 Answers

1
votes

Answering my own question

I solved this by using IValueConverter to handle the replacement of binding name string.

How to replace strings in StringFormat in WPF Binding