2X faster development
The ultimate WPF UI toolkit to boost your development speed.
WPF 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>
|
2X faster development
The ultimate WPF UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.