0

I'm aware that I can group items in a ComboBox by creating a ListCollectionView in code and setting a GroupDescription.

I'm wondering if there's a way to avoid using code behind, by populating the ListCollectionView directly in XAML.

In my case, the ComboBox has a fixed list of elements with DynamicBindings in them. So it would be easier for me to stay in XAML.

Here's the simplified example:

<UserControl.Resources>
    <x:Array x:Key="ExportItemArray" Type="{x:Type i:ExportItem}">
        <i:ExportItem Name="Audio" Description="WAV"/>
        <i:ExportItem Name="Image" Description="PNG Image"/>
        <i:ExportItem Name="Video" Description="Mp4 Video"/>
    </x:Array>

    <CollectionViewSource x:Key="CollectionViewSource" Source="{Binding ExportItemArray}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Name"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

<ComboBox ItemsSource="{StaticResource CollectionViewSource}">
    <ComboBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ComboBox.GroupStyle>
</ComboBox>

The CollectionViewSource is not being accepted by the ComboBox, since it's a proxy type of CollectionView instead of ListCollectionView.

I'm not sure if there's any proxy for ListCollectionView.

Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79
  • 2
    When I use collection view source for items source, I usually do `{Binding Source={StaticResource CollectionViewSource}}` – XAMlMAX Oct 24 '20 at 00:38
  • I understand now, thanks to you, that my `Binding` would never work. I'm a bit rusty in XAML. The `ComboBox` is not accepting the `CollectionViewSource` as its `ItemSource`. – Nicke Manarin Oct 24 '20 at 00:44
  • My issue was that I was using Binding inside the `Source` of the `CollectionViewSource` too. – Nicke Manarin Oct 24 '20 at 01:28
  • Yes, `Source="{Binding ExportItemArray}"` is also invalid. You needed `Source="{StaticResource ExportItemArray}"` there instead. But you can't use the `StaticResource` syntax for setting `ItemsSource` to a `CollectionViewSource`, and that's all you were really explicitly asking in the question, hence the duplicate. – Peter Duniho Oct 24 '20 at 01:32

0 Answers0