2014-07-07 31 views
6

我有一個DataGrid綁定到集合,我想分組。下面是代碼WPF DataGrid與總計和其他字段分組

收藏:

private string _ID; 
private string _Descript; 
private decimal _Amount; 
public string ID 
{ 
    get { return _ID; } 
    set { _ID = value; NotifyPropertyChanged("ID"); } 
} 
public decimal Amount 
{ 
    get { return _Amount; } 
    set { _Amount = value; NotifyPropertyChanged("Amount"); } 
} 
public string Descript 
{ 
    get { return _Descript; } 
    set { _Descript = value; NotifyPropertyChanged("Descript"); } 
    } 

C#;

ListCollectionView groupcollection = new ListCollectionView(myCollection); 
groupcollection.GroupDescriptions.Add(new PropertyGroupDescription("ID")); 
myDataGrid.ItemsSource = groupcollection; 

XAML:

<DataGrid Name="myDataGrid"> 
<DataGrid.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.HeaderTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding Path=Name}" /> 
       </StackPanel> 
      </DataTemplate> 
     </GroupStyle.HeaderTemplate> 
     <GroupStyle.ContainerStyle> 
      <Style TargetType="{x:Type GroupItem}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type GroupItem}"> 
          <Expander> 
           <Expander.Header> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Text="{Binding Path=Name}" Margin="5"/> 
             <TextBlock Text="Count" Margin="5" /> 
             <TextBlock Text="{Binding Path=ItemCount}" Margin="5"/> 
            </StackPanel> 
           </Expander.Header> 
           <ItemsPresenter /> 
          </Expander> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </GroupStyle.ContainerStyle> 
    </GroupStyle> 
</DataGrid.GroupStyle> 

這完美的作品,但現在在Expander.Header我要添加了一個 「量」 和 「DESCRIPT」 值的摘要。因此,例如,如果集合中有3個記錄的ID爲「ABC」,每個記錄爲20,ABC的描述爲「我的計數」,我希望看到;

ABC My Count total 60 

我該怎麼做?

回答

12

您可以使用通過組頭的Items屬性的轉換器,例如

<Window.Resources> 
    <local:GroupsToTotalConverter x:Key="groupsConverter" /> 
</Window.Resources> 

<Expander.Header> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="{Binding Path=Name}" Margin="5"/> 
     <TextBlock Text="total" Margin="5" /> 
     <TextBlock Text="{Binding Path=Items, Converter={StaticResource groupsConverter}}" Margin="5" /> 
    </StackPanel> 

其中所述轉換器執行計算,總傳回作爲字符串的文本塊:

public class GroupsToTotalConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value is ReadOnlyObservableCollection<Object>) 
     { 
      var items = (ReadOnlyObservableCollection<Object>)value; 
      Decimal total = 0; 
      foreach (GroupItem gi in items) 
      { 
       total += gi.Amount; 
      } 
      return total.ToString(); 
     } 
     return ""; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return value; 
    } 
} 

至於我建議也由分組的描述中,並編寫另一個轉換器,以與上述類似的方式從項目中提取描述。

相關問題