如何在ListBox中显示分组的分层数据

时间:2011-11-18 15:50:37

标签: wpf

我有分层数据。我需要在按每个级别分组的列表框中显示它。测试数据如下:

public class StudentAwardDisbursement {
  public string StudentName {get; set;}
  public int AwardTotal {get; set; }
  public ObservableCollection<AwardDisbursement > {get; set;}
}

public class AwardDisbursement {
  public string AcademicYearHeader {get; set; }
  public ObservableCollection<Disbursements > {get; set;}
}

public class Disbursements {
  public DateTime DisbursedOn {get; set;}
  public int DisbursedAmount {get; set;}
}

到目前为止,我最好的尝试是:

  <StackPanel x:Name="DisbursementsPanel">
     <StackPanel x:Name="StudentHeader">
        <TextBlock Text="All Disbursements for " />
        <TextBlock Text="{Binding StudentName}" />
     </StackPanel>

     <ListBox x:Name="FinanicalAwardList" DataContext="{Binding AwardDisbursement}">
        <StackPanel>
           <TextBlock x:Name="FinancialAwardHeaderText" Text="{Binding AcademicYearHeader}"/>
        </StackPanel>
        <ListBox ItemsSource="{Binding Disbursements}">
           <ListBox.ItemTemplate>
              <DataTemplate>
                 <StackPanel Orientation="Vertical">
                    <Border BorderThickness="1" BorderBrush="black" />
                    <StackPanel Orientation="Horizontal">
                       <StackPanel Orientation="Vertical">
                          <TextBlock Text="Disbursed On" />
                          <TextBlock Text="{Binding DisbursedOn}" FontWeight="Bold"/>
                          <TextBlock Text="Amount" Margin="0,6,0,0"/>
                          <TextBlock Text="{Binding DisbursedAmount }"/>
                       </StackPanel>
                 </StackPanel>
              </DataTemplate>
           </ListBox.ItemTemplate>
        </ListBox>
     </ListBox>

     <StackPanel x:Name="StudentFooter" Orientation="Horizontal" Background="DarkGray">
        <TextBlock Text="Awards: " FontSize="14" Foreground="White" Padding="6,6,0,6"/>
        <TextBlock Text="{Binding AwardTotal, StringFormat=c0}" FontSize="14" FontWeight="Bold" Foreground="White" Padding="0,6,6,6"/>
     </StackPanel>
  </StackPanel>

此示例还有一个具有学生聚合值的页脚。问题是没有进行分组。我只得到一个“FinancialHeaderText”。所有支出都在显示,但未按“AcademicYearHeader”分组。显示器的需要不允许树视图。我没有让hierarchyicaDataTemplate和CollectionViewSource工作,我猜是一个新手。我愿意再次提供足够的提示和示例。如果有帮助,这是一个只读显示。建议?

1 个答案:

答案 0 :(得分:0)

您的布局被XAML视为:

< StudentHeader >
    < ListBox: StudentAwardDisbursement.AwardDisbursements >
       < Item1: AcademicYearHeader >
       < Item2: AwardDisbursement.Disbursements > (ListBox)
          < AwardDisbursement 0 >
          < AwardDisbursement 1 >
                   ...
          < AwardDisbursement N >

错误发生在你的第一个ListBox,Item1和Item2中。它应该更像是:

< StudentHeader >
    < ListBox: StudentAwardDisbursement.AwardDisbursements >
       < ListBox.ItemTemplate > *** Inside ItemTemplate ***          
          < AcademicYearHeader >
          < ListBox AwardDisbursement.Disbursements >
             < ListBox.ItemTemplate >   
                < DataTemplate Disbursement >