【问题标题】:How can I create a group footer in a WPF ListView ( GridView )如何在 WPF ListView ( GridView ) 中创建组页脚
【发布时间】:2009-03-24 18:47:21
【问题描述】:

我有一个显示销售订单并按状态分组的 ListView。在 WinForms 中,我在每个组的底部都有一个页脚,显示每个组的总售价,我想在 WPF 中做同样的事情。

我已经知道如何对订单进行分组,但我不知道如何创建页脚。

这是我目前的组风格:

<ListView.GroupStyle>
            <GroupStyle HidesIfEmpty="True">
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <!--CollectionViewGroup.Name will be assigned the value of Status for that group.-->
                        <!--http://stackoverflow.com/questions/639809/how-do-i-group-items-in-a-wpf-listview-->
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{Binding Path=Name}" HorizontalAlignment="Left" FontWeight="Bold"  Foreground="Black"/>
                            <Line Grid.Column="1" Stroke="Black" X2="500" Fill="Black" VerticalAlignment="Center" />
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>

【问题讨论】:

    标签: wpf .net-3.5 styles


    【解决方案1】:

    如果您正在寻找这样的东西:


    (来源:bendewey.com

    然后您可以将 ContainerStyle 的 Template 属性用于 GroupStyle。在此示例中,我使用 DockPanel,您提供的 Grid 停靠在底部,ItemsPresenter 填充其余部分。此外,为了获得项目总数,您必须使用底部提供的转换器。

    Window.xaml

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.Window3"
        x:Name="Window"
        Title="Window3"
        xmlns:local="clr-namespace:WpfApplication1"
        Width="640" Height="480">
        <Window.Resources>
            <local:MyDataSource x:Key="MyData" />
            <CollectionViewSource x:Key="ViewSource" Source="{Binding Source={StaticResource MyData}, Path=Users}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Country" />
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Window.Resources>
        <Grid x:Name="LayoutRoot">
          <ListView ItemsSource="{Binding Source={StaticResource ViewSource}}">
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                          <DockPanel>
                                            <Grid DockPanel.Dock="Bottom">
                                                <Grid.Resources>
                                                    <local:TotalSumConverter x:Key="sumConverter" />
                                                </Grid.Resources>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*" />
                                                    <ColumnDefinition Width="*" />
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                </Grid.RowDefinitions>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Grid.Column="0" Text="Total: " FontWeight="Bold"/>
                                                    <TextBlock Grid.Column="0" Text="{Binding Path=Name}" />
                                                </StackPanel>
                                                <Line Grid.Column="1" Stroke="Black" X2="500" Fill="Black" VerticalAlignment="Center" />
    
                                                <TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" />
                                            </Grid>
                                        <ItemsPresenter />
                                    </DockPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
            <ListView.View>
               <GridView>
                <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Width="140" Header="Phone Number" DisplayMemberBinding="{Binding Phone}"/>
                <GridViewColumn Width="140" Header="Country" DisplayMemberBinding="{Binding Country}" />
                <GridViewColumn Width="140" Header="Total" DisplayMemberBinding="{Binding Total}" />
               </GridView>
              </ListView.View>
          </ListView>
    
        </Grid>
    </Window>
    

    MyDataSource.cs

    public class MyDataSource
    {
        public ObservableCollection<User> Users { get; set; }
    
        public MyDataSource()
        {
            Users = new ObservableCollection<User>();
            LoadDummyData();
        }
    
        private void LoadDummyData()
        {
            Users.Add(new User()
            {
                Name = "Frank",
                Phone = "(122) 555-1234",
                Country = "USA",
                Total = 432
            });
    
            Users.Add(new User()
            {
                Name = "Bob",
                Phone = "(212) 555-1234",
                Country = "USA",
                Total = 456
            });
    
            Users.Add(new User()
            {
                Name = "Mark",
                Phone = "(301) 555-1234",
                Country = "USA",
                Total = 123
            });
    
            Users.Add(new User()
            {
                Name = "Pierre",
                Phone = "+33 (122) 555-1234",
                Country = "France",
                Total = 333
            });
    
            Users.Add(new User()
            {
                Name = "Jacques",
                Phone = "+33 (122) 555-1234",
                Country = "France",
                Total = 222
            });
    
            Users.Add(new User()
            {
                Name = "Olivier",
                Phone = "+33 (122) 555-1234",
                Country = "France",
                Total = 444
            });
        }
    }
    

    用户.cs

    public class User
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Country { get; set; }
        public double Total { get; set; }
    }
    

    TotalSumConverter.cs

    public class TotalSumConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var users = value as IEnumerable<object>;
            if (users == null)
                return "$0.00";
    
            double sum = 0;
    
            foreach (var u in users)
            {
                sum += ((User)u).Total;
            }
    
    
            return sum.ToString("c");
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
    

    【讨论】:

    • 非常感谢您在回答中付出了这么多努力。真的很有帮助。
    • 我很高兴它对你有用。我自己写了一些东西。
    • 我将这个 ControlTemplate 用于 DataGrid,效果很好。
    • 很好的解决方案!任何想法如何在所有组下轻松添加“总计”行? (显示总和的行)
    • 如果 ItemsSource 集合中的项目发生更改,如何更新 Sum 上的绑定?
    猜你喜欢
    • 2011-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多