|
1 | | -# How to define summary rows using AttachedProperty in WPF and UWP DataGrid (SfDataGrid)? |
| 1 | +# How to Define Summary Rows using AttachedProperty in WPF / UWP DataGrid? |
2 | 2 |
|
3 | | -This example illustrates how to define summary rows using [AttachedProperty](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/properties/attached-properties-overview) in [WPF DataGrid](https://www.syncfusion.com/wpf-ui-controls/datagrid) (SfDataGrid). |
| 3 | +This example illustrates how to define summary rows using [AttachedProperty](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/properties/attached-properties-overview) in [WPF DataGrid](https://www.syncfusion.com/wpf-ui-controls/datagrid) and [UWP DataGrid](https://www.syncfusion.com/uwp-ui-controls/datagrid) (SfDataGrid). |
4 | 4 |
|
5 | | -[WPF DataGrid](https://www.syncfusion.com/wpf-ui-controls/datagrid) (SfDataGrid) provides support to show the column summary. If the DataContext of `SfDataGrid` is ViewModel, you can bind [SummaryColumns](http://help.syncfusion.com/cr/cref_files/wpf/Syncfusion.SfGrid.WPF~Syncfusion.UI.Xaml.Grid.GridSummaryRow~SummaryColumns.html) to a property in ViewModel using the [AttachedProperty](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/attached-properties-overview) of type `List<GridSummaryColumn>`. |
| 5 | +DataGrid provides support to show the column summary. If the DataContext of DataGrid is ViewModel, you can bind [SummaryColumns](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.GridSummaryRow.html#Syncfusion_UI_Xaml_Grid_GridSummaryRow_SummaryColumns) to a property in ViewModel using the AttachedProperty of type List <GridSummaryColumn>. |
6 | 6 |
|
7 | | -Refer to the code example in the following KB article to define an `AttachedProperty` of type `List<GridSummaryColumn>`. |
| 7 | +Refer to the following code example to define the AttachedProperty of type List <GridSummaryColumn>. |
8 | 8 |
|
9 | | -KB article - [How to define summary rows using AttachedProperty in WPF and UWP DataGrid (SfDataGrid)](https://www.syncfusion.com/kb/9838/how-to-define-summary-rows-using-attached-property-in-wpf-datagrid-sfdatagrid) |
| 9 | +#### C# |
| 10 | + |
| 11 | +``` csharp |
| 12 | +public class SfDataGridAttachedProperty |
| 13 | +{ |
| 14 | + public static readonly DependencyProperty DynamicSummaryColumnsProperty = DependencyProperty.RegisterAttached("DynamicSummaryColumns", |
| 15 | + typeof(List<GridSummaryColumn>), |
| 16 | + typeof(SfDataGridAttachedProperty) |
| 17 | + , new FrameworkPropertyMetadata(null, OnDynamicSummaryColumnsChanged)); |
| 18 | + |
| 19 | + public static void SetDynamicSummaryColumns(UIElement element, List<GridSummaryColumn> value) |
| 20 | + { |
| 21 | + element.SetValue(DynamicSummaryColumnsProperty, value); |
| 22 | + } |
| 23 | + public static List<GridSummaryColumn> GetDynamicSummaryColumns(UIElement element) |
| 24 | + { |
| 25 | + return (List<GridSummaryColumn>)element.GetValue(DynamicSummaryColumnsProperty); |
| 26 | + } |
| 27 | + |
| 28 | + private static void OnDynamicSummaryColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args) |
| 29 | + { |
| 30 | + SfDataGrid grid = d as SfDataGrid; |
| 31 | + |
| 32 | + if (grid.TableSummaryRows.Count() > 1) |
| 33 | + { |
| 34 | + grid.TableSummaryRows.Clear(); |
| 35 | + } |
| 36 | + |
| 37 | + GridTableSummaryRow gsr = new GridTableSummaryRow(); |
| 38 | + gsr.ShowSummaryInRow = false; |
| 39 | + |
| 40 | + var list = ((List<GridSummaryColumn>)args.NewValue); |
| 41 | + |
| 42 | + foreach (var item in list) |
| 43 | + { |
| 44 | + gsr.SummaryColumns.Add(item); |
| 45 | + } |
| 46 | + |
| 47 | + grid.TableSummaryRows.Add(gsr); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Refer to the following code example to populate GridSummaryColumn in Viewmodel. |
| 53 | + |
| 54 | +#### C# |
| 55 | + |
| 56 | +``` csharp |
| 57 | +internal class ViewModel : INotifyPropertyChanged |
| 58 | +{ |
| 59 | + private List<GridSummaryColumn> _summarycols; |
| 60 | + |
| 61 | + public List<GridSummaryColumn> SummaryColumns |
| 62 | + { |
| 63 | + get { return _summarycols; } |
| 64 | + set |
| 65 | + { |
| 66 | + _summarycols = value; |
| 67 | + RaisePropertyChanged("SummaryColumns"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public ViewModel() |
| 72 | + { |
| 73 | + PopulateEmployeeDetails(); |
| 74 | + SetSummaryColumns(); |
| 75 | + } |
| 76 | + |
| 77 | + private void SetSummaryColumns() |
| 78 | + { |
| 79 | + SummaryColumns = new List<GridSummaryColumn>(); |
| 80 | + SummaryColumns.Add(new GridSummaryColumn() |
| 81 | + { |
| 82 | + MappingName = "EmployeeID", |
| 83 | + Name = "EmployeeID", |
| 84 | + SummaryType = SummaryType.CountAggregate, |
| 85 | + Format = "Total: {Count}" |
| 86 | + }); |
| 87 | + SummaryColumns.Add(new GridSummaryColumn() |
| 88 | + { |
| 89 | + MappingName = "EmployeeSalary", |
| 90 | + Name = "EmployeeSalary", |
| 91 | + SummaryType = SummaryType.DoubleAggregate, |
| 92 | + Format = "Total: {Sum}" |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +Refer to the following code example to bind the attached property in DataGrid. |
| 99 | + |
| 100 | +#### XAML |
| 101 | + |
| 102 | +``` xml |
| 103 | +<Syncfusion:SfDataGrid x:Name="sfdatagrid" |
| 104 | + local:SfDataGridAttachedProperty.DynamicSummaryColumns="{Binding SummaryColumns}" |
| 105 | + ItemsSource="{Binding EmployeeDetails}"> |
| 106 | +</Syncfusion:SfDataGrid> |
| 107 | +``` |
0 commit comments