WPF C# Display ICollectionView Groupings Only

64 views Asked by At

I have an ICollectionView with a GroupDescriptions applied. I would like to display the group in the ListView and NOT display the members of the group.

I would also like to use a property from the ObservableCollection to bind to the BackgroundColor.

I attached a image to help clarify. The Red text is the correct group name that I want to display. I would also like to bind the BackgroundColor to each group name (as it is in the ItemTemplate) AND I would like to eliminate the ItemTemplate completely (which I can't do now without it showing the default binding).

enter image description here

C#

    private string _WorkOrderDisplayName;
    public string WorkOrderDisplayName
    {
        get { return _WorkOrderDisplayName; }
        set { _WorkOrderDisplayName = value; NotifyPropertyChanged("WorkOrderDisplayName"); }
    }

    private string _BackgroundColor;
    public string BackgroundColor
    {
        get { return _BackgroundColor; }
        set
        {
            _BackgroundColor = value;
            NotifyPropertyChanged("BackgroundColor");
        }
    }


//WorkOrdersICollectionView
WorkOrdersGroupingICollectionView = new CollectionViewSource { Source = AllEstimateItemsForJobObsCollection }.View;
WorkOrdersGroupingICollectionView.GroupDescriptions.Add(new PropertyGroupDescription("WorkOrderDisplayName"));

WPF

<ListView ItemsSource="{Binding WorkOrdersGroupingICollectionView}">

                            <ListView.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <TextBlock  Text="{Binding Name}" FontSize="16" Foreground="Red" Background="{Binding BackgroundColor}"/>
                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ListView.GroupStyle>

                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock  Text="{Binding WorkOrderDisplayName}" MinWidth="155" Background="{Binding BackgroundColor}" Foreground="Black" Padding="8 3 8 3" FontSize="14" />
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

Revised with ContainerStyle. Just need BackgroundColor to bind correctly (this doesn't work as is):

<ListView Grid.Row="1" Name="WorkOrderGroupsList" 
          ItemsSource="{Binding WorkOrdersGroupingICollectionView}">

                            <ListView.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.ContainerStyle>
                                        <Style TargetType="{x:Type GroupItem}">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                                        <TextBlock  Text="{Binding Name}" FontSize="16" Foreground="Red" Background="{Binding BackgroundColor}" Margin="3"/>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </GroupStyle.ContainerStyle>
                                </GroupStyle>
                            </ListView.GroupStyle>

                        </ListView>
1

There are 1 answers

0
mm8 On

You should be able to bind to the BackgroundColor property of the first item in each group like this:

<ControlTemplate TargetType="{x:Type GroupItem}">
    <TextBlock  Text="{Binding Name}" FontSize="16" Foreground="Red" 
                Background="{Binding Items[0].BackgroundColor}" Margin="3"/>
</ControlTemplate>