【问题标题】:Group heading is not displayed after grouping in ItemsControl在 ItemsControl 中分组后不显示组标题
【发布时间】:2017-06-03 20:34:17
【问题描述】:

我正在尝试使用 CollectionViewSource 对我的 ObservableCollection 进行分组,它似乎适用于项目,但它没有显示 GroupBox 的绑定属性值。

这是我正在尝试的:

我有 List<Object> 包含属性描述,季节。我想按季节分组。这是我的xml:

<mah:MetroWindow x:Class="eCatalog_Launcher.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:eCatalog_Launcher"
             xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
             mc:Ignorable="d"
             Title="eCatalog Launcher"
             WindowState="Maximized"
             Loaded="MetroWindow_Loaded"
             Closing="MetroWindow_Closing">

<Window.Resources>
    <CollectionViewSource x:Key="catalogsBySeasons"
                          Source="{Binding Path=Catalogs, Mode=TwoWay}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Season" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</Window.Resources>

<ScrollViewer>
    <ItemsControl ItemsSource="{Binding Source={StaticResource catalogsBySeasons}}">
        <ItemsControl.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <GroupBox Header="{Binding Season}">
                                        <ItemsPresenter />
                                    </GroupBox>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ItemsControl.GroupStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <mah:Tile Title="{Binding Description}"
                          Tag="{Binding}"
                          Style="{StaticResource SmallTileStyle}"
                          Click="Tile_Click">
                    <iconPacks:PackIconMaterial Width="32"
                                                Height="32"
                                                Margin="0, -30, 0, 0"
                                                Kind="{Binding Kind}">
                    </iconPacks:PackIconMaterial>
                </mah:Tile>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</StackPanel>

它将季节值显示为GroupBox 标题。 有什么问题吗?

【问题讨论】:

    标签: c# wpf mvvm data-binding observablecollection


    【解决方案1】:

    您应该绑定到组的 Name 属性:

    <ControlTemplate TargetType="{x:Type GroupItem}">
        <GroupBox Header="{Binding Name}">
            <ItemsPresenter />
        </GroupBox>
    </ControlTemplate>
    

    这应该显示您分组依据的 Season 属性的实际值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多