我想在这个WPF应用程序中实现的目的是总结Header上按族分组的列“size”的所有值。数据网格中是否有任何方法可以处理在特定类别下分组的所有行的列的总和?
XAML
<Window.Resources>
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander x:Name="exp" IsExpanded="False" Background="White" Foreground="Black">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Name="TotalSize" Text="Total size here" Margin="290 0 0 0" />
</StackPanel>
</Expander.Header>
<ItemsPresenter Margin="20 0 0 0" />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
C#
public partial class MainWindow : Window
{
ObservableCollection<DownloadManager> dManager = new ObservableCollection<DownloadManager>();
public MainWindow()
{
InitializeComponent();
XmlDocument doc = new XmlDocument();
doc.Load("dm.xml");
XmlNodeList xnList = doc.SelectNodes("/root/repository/MEDIA");
foreach (XmlNode media in xnList)
{
string PRODUCT = media["PRODUCT"].InnerText;
string SIZE = media["SIZE"].InnerText;
string MEDIASTATUS = media["MEDIASTATUS"].InnerText;
string FAMILY = media["FAMILY"].InnerText;
dManager.Add(new DownloadManager { Name = PRODUCT, Size = SIZE, Description = MEDIASTATUS, Family = FAMILY });
}
ListCollectionView collection = new ListCollectionView(dManager);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Family"));
dataGrid.ItemsSource = collection;
}
}
public class DownloadManager
{
public string Name { get; set; }
public string Size { get; set; }
public string Description { get; set; }
public string Family { get; set; }
}
XML
<root>
<repository >
<MEDIA>
<PRODUCT>MS Office</PRODUCT>
<FAMILY>Paid</FAMILY>
<SIZE>1056</SIZE>
<MEDIASTATUS>LIVE</MEDIASTATUS>
</MEDIA>
<MEDIA>
<PRODUCT>Matlab</PRODUCT>
<FAMILY>Paid</FAMILY>
<SIZE>2096</SIZE>
<MEDIASTATUS>LIVE</MEDIASTATUS>
</MEDIA>
<MEDIA>
<PRODUCT>Winzip</PRODUCT>
<FAMILY>Free</FAMILY>
<SIZE>119</SIZE>
<MEDIASTATUS>LIVE</MEDIASTATUS>
</MEDIA>
<MEDIA>
<PRODUCT>Team Viewer</PRODUCT>
<FAMILY>Free</FAMILY>
<SIZE>256</SIZE>
<MEDIASTATUS>LIVE</MEDIASTATUS>
</MEDIA>
<MEDIA>
<PRODUCT>Coral Draw</PRODUCT>
<FAMILY>Paid</FAMILY>
<SIZE>3110</SIZE>
<MEDIASTATUS>LIVE</MEDIASTATUS>
</MEDIA>
<MEDIA>
<PRODUCT>Photoshop</PRODUCT>
<FAMILY>Free</FAMILY>
<SIZE>2056</SIZE>
<MEDIASTATUS>LIVE</MEDIASTATUS>
</MEDIA>
</repository>
</root>
答案 0 :(得分:3)
我能想到的最干净的解决方案是创建一个Converter
类 - 实现Convert
方法循环分组项,汇总Size
值,并格式化返回字符串根据需要:
public class SumGroupConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
GroupItem groupItem = value as GroupItem;
CollectionViewGroup collectionViewGroup = groupItem.Content as CollectionViewGroup;
int sum = 0;
foreach (var item in collectionViewGroup.Items)
{
DownloadManager dman = item as DownloadManager;
int size = 0;
int.TryParse(dman.Size, out size);
sum += size;
}
return string.Format("Total size: {0}", sum);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后,假设您的xaml中有一行xmlns:local="clr-namespace:WpfApplication1"
,那么您可以将以下内容添加到Window.Resources
:
<local:SumGroupConverter x:Key="SumTitle"/>
TextBox
中的Expander
:
<TextBlock
Name="TotalSize"
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupItem}}, Converter={StaticResource ResourceKey=SumTitle}}"
Margin="290 0 0 0"/>
结果: