如何在数据网格行中添加分组行的所有列值 header
How to add all the column values of grouped rows in data grid row header
我试图在此 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>
我能想到的最简洁的解决方案是创建一个 Converter
class - 实施 Convert
方法循环分组项目,对 Size
值求和,并根据需要格式化 return 字符串:
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"/>
而对于 Expander
中的 TextBox
:
<TextBlock
Name="TotalSize"
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupItem}}, Converter={StaticResource ResourceKey=SumTitle}}"
Margin="290 0 0 0"/>
结果:
我试图在此 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>
我能想到的最简洁的解决方案是创建一个 Converter
class - 实施 Convert
方法循环分组项目,对 Size
值求和,并根据需要格式化 return 字符串:
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"/>
而对于 Expander
中的 TextBox
:
<TextBlock
Name="TotalSize"
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupItem}}, Converter={StaticResource ResourceKey=SumTitle}}"
Margin="290 0 0 0"/>
结果: