2016-05-04 101 views
2

我已经提出了一个允许获得比赛实况结果的应用程序。 现在每场比赛的组织是这样的:如何插入多个GroupDescription?

Nation->League->Match list 

所以我的Nation那是League的容器,和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。然后我在ListView(XAML控件)的ItemsSource中插入items

对于创建头组为Nation名字,我用了一个CollectionView

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource); 
     PropertyGroupDescription groupNations = new PropertyGroupDescription("Name"); 

此代码允许我创建多个标题,而结果是我想要的东西。我也做了同样的League,“导致我需要的是插入一个sub-headerLeagueNation容器(头):

 view.GroupDescriptions.Add(groupNations); 
     PropertyGroupDescription groupCompetions = new PropertyGroupDescription("League"); 
     view.GroupDescriptions.Add(groupNations); 

,但实际上这是结果:

enter image description here

如何仅显示国家标题,但League标题不显示在Nation标题内。我究竟做错了什么?

+0

你怎么确定是在里面而不是在下面。如果你关闭国家,它会消失吗? – Paparazzi

+0

@Paparazzi不确定,但可能是另一个问题。现在我无法打印联盟的名字。 – Dillinger

回答

2

您可以通过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,仅有1线:

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

{Binding Name}在您的原始代码中是{Binding League}

+0

所以你还添加了一个新的XAML'GroupStyle'?你能否显示已经改变的代码?我想我会在将来用类替换结构体。但是,为什么你不建议结构?与类相比,结构是一种轻量级的数据。结果就是这样:http://i.stack.imgur.com/JuFRz.png我添加的唯一的东西是联盟。 – Dillinger

+0

@Dillinger我加了代码...但是这不是一个好的方法,首先这样做,这就是为什么我最初没有添加它。这是一个“黑客”。 – svinja

+0

非常感谢,你有什么建议?我没有找到任何控件允许我这样做,用vb.net和windows窗体创建了这个自定义控件:http://pastebin.com/phrjz2un但现在我无法重用它。 – Dillinger