如何插入多个GroupDescription?

时间:2016-05-04 11:19:44

标签: c# wpf xaml

我做了一个允许获得比赛实时结果的应用程序。 现在每场比赛的组织是这样的:

Nation->League->Match list

所以我NationLeague的容器,而LeagueMatch的容器。我首先做的是创建一个XAML结构,允许我实现之前所说的容器结果:

<Window x:Class="GroupBox_Header.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:GroupBox_Header"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid Margin="10">
    <ListView Name="lvUsers">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Home Team" Width="50" DisplayMemberBinding="{Binding HomeTeam}" />
                <GridViewColumn Header="Away Team" Width="50" DisplayMemberBinding="{Binding AwayTeam}" />
            </GridView>
        </ListView.View>

        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True">
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                                <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                                <TextBlock Text=" Items" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True">
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding League}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
</Grid>

如何看待有ListView定义的GroupStyle,在此我保存了所有Nation。您可以在演示解决方案中尝试XAML并检查结果。

在此之后,我创建了允许我存储值的结构:

public struct League 
{
    public string Name { get; set; }
}

public struct Country
{
    public string Name   { get; set; }
    public League League;
}

非常简单,一个用于保存所有联赛的League结构和一个用于国家的Country结构。我以这种方式对Country进行了定价:

List<Country> items = new List<Country>();
            items.Add(new Country() { Name = "Italy", League = { Name = "Serie A"} });
            items.Add(new Country() { Name = "Italy", League = { Name = "Serie B" } });
            items.Add(new Country() { Name = "England", League = { Name = "Premiere League" } });
            items.Add(new Country() { Name = "Spain", League = { Name = "Primeira Division" } });
            lvUsers.ItemsSource = items; 

所以我实际上在列表Italy (2), England, Spain中。然后我在items的{​​{1}}(XAML控件)中插入了ItemsSource

要创建标题组作为ListView名称,我使用了Nation

CollectionView

这段代码允许我创建多个标题,结果正是我想要的。我也为CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource); PropertyGroupDescription groupNations = new PropertyGroupDescription("Name"); 做了同样的事情,因为我需要在League容器(标题)中插入sub-header League名称:

Nation

但实际上这就是结果:

enter image description here

如何只显示国家标题,但 view.GroupDescriptions.Add(groupNations); PropertyGroupDescription groupCompetions = new PropertyGroupDescription("League"); view.GroupDescriptions.Add(groupNations); 标题中未显示League标题。我做错了什么?

1 个答案:

答案 0 :(得分:2)

你可以修复&#34;它通过使League属性(添加getter和setter - WPF绑定附加到属性,而不是字段),使第二组描述组由"League.Name"而不是"League"而成为第二组组样式TextBlock绑定绑定到Name,而不是League

然后你明白了:

enter image description here

但我会说你正在以错误的方式接近这个并以非常奇怪的方式对数据进行建模。如果ListView旨在列出Matches,那么您应该绑定到Match个对象的集合(或对它们的视图),并且每个Match都应该引用其父项League,每个League都应引用其父Country

或者,使用树而不是列表,然后使用包含子Country的集合的Leagues和包含子League的集合的每个Matches

或两者兼有 - League包含Matches的集合,但每个匹配都有League个引用,Country/League类似。

它在您的代码中的方式有​​点奇怪。

另外,我不明白为什么你使用结构代替你的对象。

后面的代码更改(注意,我并不是说这是一个很好的方法,见上文):

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        List<Country> items = new List<Country>();
        items.Add(new Country() { Name = "Italy", League = new League { Name = "Serie A" } }); // Added "new League"
        items.Add(new Country() { Name = "Italy", League = new League { Name = "Serie B" } });
        items.Add(new Country() { Name = "England", League = new League { Name = "Premiere League" } });
        items.Add(new Country() { Name = "Spain", League = new League { Name = "Primeira Division" } });
        lvUsers.ItemsSource = items;

        CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
        PropertyGroupDescription groupNations = new PropertyGroupDescription("Name");
        view.GroupDescriptions.Add(groupNations);
        PropertyGroupDescription groupCompetions = new PropertyGroupDescription("League.Name"); // Changed "League" to "League.Name"
        view.GroupDescriptions.Add(groupCompetions); // Fixed the variable name here

        lvUsers.ItemsSource = view; // Added, you probably have it but didn't post it
    }
}

public struct League
{
    public string Name { get; set; }
}

public struct Country
{
    public string Name { get; set; }
    public League League { get; set; } // added getter and setter
}

对XAML的更改,只有一行:

<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />

{Binding Name}在原始代码中为{Binding League}