How to define summary rows using attached property in WPF DataGrid ?
tWPF DataGrid (SfDataGrid) provides support to show the column summary. If the DataContext of SfDataGrid is ViewModel, you can bind SummaryColumns to a property in ViewModel using the AttachedProperty of type List<GridSummaryColumn>.
Refer to the following code example to define the AttachedProperty of type List<GridSummaryColumn>.
public class SfDataGridAttachedProperty { public static readonly DependencyProperty DynamicSummaryColumnsProperty = DependencyProperty.RegisterAttached("DynamicSummaryColumns", typeof(List<GridSummaryColumn>), typeof(SfDataGridAttachedProperty) , new FrameworkPropertyMetadata(null, OnDynamicSummaryColumnsChanged)); public static void SetDynamicSummaryColumns(UIElement element, List<GridSummaryColumn> value) { element.SetValue(DynamicSummaryColumnsProperty, value); } public static List<GridSummaryColumn> GetDynamicSummaryColumns(UIElement element) { return (List<GridSummaryColumn>)element.GetValue(DynamicSummaryColumnsProperty); } private static void OnDynamicSummaryColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args) { SfDataGrid grid = d as SfDataGrid; if (grid.TableSummaryRows.Count() > 1) { grid.TableSummaryRows.Clear(); } GridTableSummaryRow gsr = new GridTableSummaryRow(); gsr.ShowSummaryInRow = false; var list = ((List<GridSummaryColumn>)args.NewValue); foreach (var item in list) { gsr.SummaryColumns.Add(item); } grid.TableSummaryRows.Add(gsr); } }
Refer to the following code example to populate GridSummaryColumn in Viewmodel.
internal class ViewModel : INotifyPropertyChanged { private List<GridSummaryColumn> _summarycols; public List<GridSummaryColumn> SummaryColumns { get { return _summarycols; } set { _summarycols = value; RaisePropertyChanged("SummaryColumns"); } } public ViewModel() { PopulateEmployeeDetails(); SetSummaryColumns(); } private void SetSummaryColumns() { SummaryColumns = new List<GridSummaryColumn>(); SummaryColumns.Add(new GridSummaryColumn() { MappingName = "EmployeeID", Name = "EmployeeID", SummaryType = SummaryType.CountAggregate, Format = "Total: {Count}" }); SummaryColumns.Add(new GridSummaryColumn() { MappingName = "EmployeeSalary", Name = "EmployeeSalary", SummaryType = SummaryType.DoubleAggregate, Format = "Total: {Sum}" }); } }
Refer to the following code example to bind the attached property in SfDataGrid.
<Syncfusion:SfDataGrid x:Name="sfdatagrid" local:SfDataGridAttachedProperty.DynamicSummaryColumns="{Binding SummaryColumns}" ItemsSource="{Binding EmployeeDetails}"> </Syncfusion:SfDataGrid>
View WPF DataGrid Summaries Demo in GitHub.
Conclusion
I hope you enjoyed learning about how to define summary rows using attached property in WPF DataGrid.
You can refer to our WPF DataGridWPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!