WPF C#仅显示ICollectionView分组

时间:2019-06-12 15:04:52

标签: c# wpf grouping observablecollection icollectionview

我有一个应用了GroupDescriptions的ICollectionView。我想在ListView中显示该组,而不显示该组的成员。

我还想使用ObservableCollection中的属性绑定到BackgroundColor。

我附上一张图片以帮助澄清。红色文本是我要显示的正确的组名。我还想将BackgroundColor绑定到每个组名(就像它在ItemTemplate中一样),并且我想完全消除ItemTemplate(如果没有它显示默认绑定,我现在就不能这样做)。

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>

与ContainerStyle一起修订。只需要BackgroundColor正确绑定即可(这不能按原样工作):

<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 个答案:

答案 0 :(得分:0)

您应该能够像这样绑定到每个组中第一项的BackgroundColor属性:

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