0

My object has property that stores more strings separated by separator. I want to display list of such objects in WPF listbox with grouping enabled. What I need is to have groups according to substrings.

Object1: Property = "string1;string2;string3"

Object2: Property = "string2;string3"

I expect listbox to be displayed like that:

string1

Object 1

Object 2

string2

Object 1

string3

Object 1

Object 2

Is that possible?

Thank you for your help.

Dusan Kocurek
  • 445
  • 3
  • 8
  • 22

1 Answers1

1

Create a wrapper class.

class MyGroup
{
 public string GroupID;
 public object SomeObject;
}

Build your flat list of that wrapper class.

private List<MyGroup> BuildItemsSourceTogether()
{
 List<MyGroup>itemsSource = new List<MyGroup>();
 foreach(var obj in myListWithObjectsWhichPropertiesAreStringsWhichFuthermoreContainSubstrings)
 {
   var stringArray = obj.Property123.Split(';');
   foreach(var str in stringArray)
   {
    itemsSource.Add(new MyGroup () { GroupID = str, SomeObject = obj});
   }
 }
 return itemsSource;
}

Tell what property in your wrapper class should be used for grouping.

class Window1
{
  public Window1()
  {
    InitalizeComponents();
    var finalData = new ListCollectionView(BuildItemsSourceTogether());
    finalData.GroupDescriptions.Add(new PropertyGroupDescription("GroupID"));
    this.DataContext = finalData;
  }
}

In XAML set the look of your groups and let WPF group that flat list of IDs for you.

I used a DataGrid with columns. You can use a ListBox.

<DataGrid ItemsSource="{Binding}">
      <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                          <TextBlock Text="{Binding Path=Name}" />
                                          <TextBlock Text="{Binding Path=ItemCount}"/>
                                          <TextBlock Text="Items"/>
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Header="header1" Binding="{Binding SomeObject.Property321}" />
        <DataGridTextColumn Header="header2" Binding="{Binding SomeObject.Property678}" />
    </DataGrid.Columns>
</DataGrid>

Have fun.

snowy hedgehog
  • 652
  • 1
  • 7
  • 23