【问题标题】:WPF C# Display ICollectionView Groupings OnlyWPF C# 仅显示 ICollectionView 分组
【发布时间】:2019-06-12 15:04:52
【问题描述】:

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

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

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

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>

【问题讨论】:

  • 你快到了,只需覆盖GroupStyle.ContainerStyle。如果你不在那里使用ItemsPresenter,那么你将看不到任何项目。
  • 谢谢!我不确定该怎么做。可以举个例子吗?
  • 有很多教程,例如here。如果成功,请发布答案。
  • 我让 ContainerStyle 工作了。问题的另一部分是如何将背景颜色绑定到组?我怀疑 BackgroundColor 需要以某种方式包含在 c#“GroupDescriptions”中,但我不确定......

标签: c# wpf grouping observablecollection icollectionview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 2011-01-09
    • 2020-05-20
    相关资源
    最近更新 更多