2013-03-25 28 views
2

我有ListCollectionView這是使用PropertyGroupDescription分組實例如下圖所示ListCollectionView - GroupDescriptions - 如何獲得分組項目數量?

ListCollectionView view = new ListCollectionView(calls); 
view.GroupDescriptions.Add(new PropertyGroupDescription("Technician.Name")); 
DGCalls.ItemsSource = view; 

分組工作正常,但我想顯示分組的項目的數量,如下圖所示。

enter image description here

是否有可能顯示計數?

這是我創建的示例應用程序。

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      Technician tech1 = new Technician() { Name = "Tech01" }; 
      Technician tech2 = new Technician() { Name = "Tech02" }; 
      List<ServiceCall> calls = new List<ServiceCall>(); 
      calls.Add(new ServiceCall() { ID = 1, Technician = tech1}); 
      calls.Add(new ServiceCall() { ID = 2, Technician = tech1 }); 
      calls.Add(new ServiceCall() { ID = 3, Technician = tech1 }); 
      calls.Add(new ServiceCall() { ID = 4, Technician = tech2 }); 
      calls.Add(new ServiceCall() { ID = 5, Technician = tech2 }); 
      ListCollectionView view = new ListCollectionView(calls); 
      view.GroupDescriptions.Add(new PropertyGroupDescription("Technician.Name")); 
      DGCalls.ItemsSource = view; 
     } 
    } 
    public class ServiceCall 
    { 
     public int ID { get; set; } 
     public Technician Technician { get; set; } 
    } 
    public class Technician 
    { 
     public String Name { get; set; } 
    } 

} 

XAML:

<Grid> 
    <Grid.Resources> 
     <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type GroupItem}"> 
         <Expander IsExpanded="True" Background="Blue" > 
          <Expander.Header> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock Text="{Binding Name}" Foreground="White" FontWeight="Bold"/> 
            <TextBlock Text="{Binding Count}" Foreground="White" FontWeight="Bold"/> 
           </StackPanel> 
          </Expander.Header> 
          <ItemsPresenter/> 
         </Expander> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Grid.Resources> 
    <DataGrid Name="DGCalls" AutoGenerateColumns="False" IsReadOnly="True"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Call ID" Binding="{Binding ID}"/> 
      <DataGridTextColumn Header="Technician Name" Binding="{Binding Technician.Name}"/> 
     </DataGrid.Columns> 
     <DataGrid.GroupStyle> 
      <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}"> 
       <GroupStyle.Panel> 
        <ItemsPanelTemplate> 
         <DataGridRowsPresenter/> 
        </ItemsPanelTemplate> 
       </GroupStyle.Panel> 
      </GroupStyle> 
     </DataGrid.GroupStyle> 
    </DataGrid> 
</Grid> 

回答

1

只需使用CollectionViewSource的ItemCount屬性:

 <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type GroupItem}"> 
        <Expander IsExpanded="True" Background="Blue" > 
         <Expander.Header> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding Name}" Foreground="White" FontWeight="Bold"/> 
           <TextBlock Text="{Binding ItemCount}" Foreground="White" FontWeight="Bold"/> 
          </StackPanel> 
         </Expander.Header> 
         <ItemsPresenter/> 
        </Expander> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
相關問題