WPF ListView 分组 Grouping

在Resource里定义数据源和分组字段:

<CollectionViewSource x:Key="listData" Source="{Binding CategoryItemsView}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="CategoryType"></PropertyGroupDescription>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

分组字段也可以在Loaded事件中用代码定义:

        private void FileManageCard_Loaded(object sender, RoutedEventArgs e)
        {
            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvItemsCategory.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("CategoryType");
            view.GroupDescriptions.Add(groupDescription);
        }

 

然后

 

复制代码
<ListView Grid.Row="0"
                        x:Name="lvItemsCategory"
                        ItemsSource="{Binding Source={StaticResource listData}}"
                        Style="{StaticResource FileManagerListViewStyle}"
                        ItemContainerStyle="{StaticResource FileManagerListViewItemStyle}"
                        SelectionMode="Single">
                        <ListView.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal" Background="Transparent" Height="35">
                                            <TextBlock Text="{Binding Name}" FontSize="18" FontWeight="Bold" Foreground="Gray"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                            </GroupStyle>
                        </ListView.GroupStyle>
                    </ListView>
复制代码

 

 

最后附上Style和ItemContainerStyle:

复制代码
    <Style x:Key="FileManagerListViewStyle" TargetType="{x:Type ListView}">
        <Setter Property="VerticalAlignment" Value="Top"></Setter>
        <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
        <Setter Property="AllowDrop" Value="{Binding IsAllowDrop}"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListView}">
                    <ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalAlignment="Stretch">
                        <ItemsPresenter/>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="FileManagerListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
        <Setter Property="Cursor" Value="Hand"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border x:Name="back" Background="Transparent" Margin="0,0,0,0" Height="25">
                        <Grid Margin="20,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Image  Grid.Column="0"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Width="20"
                                Height="20"
                                Source="{Binding Icon,Converter={StaticResource ImgPathToImageConverter}}"/>
                            <DockPanel Grid.Column="1">
                                <TextBlock Text="{Binding DisplayName}"
                                    Height="25"
                                    Foreground="#FF6D6D6D"
                                    FontSize="16"/>
                                <TextBlock Text="{Binding Count,StringFormat=' ({0})'}"
                                    Foreground="#FF6D6D6D"
                                    FontSize="16"></TextBlock>
                            </DockPanel>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="back" Property="Background" Value="LightGray"></Setter>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="back" Property="Background" Value="Black"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
复制代码

 

posted on   lopengye  阅读(4399)  评论(1编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示