The Infragistics XamDataGrid has some powerful Summary features which allow you to use common or custom aggregate features and format and display them in various locations in the grid. But I thought one of our users had defeated them with this request.
"I want to total all the values in a child band and display the result under a totally unrelated column in the parent band".
There are two problems with this requirement..
- We want to display data from one band in the summary for a different band
- We want to display data from one column in the summary for a different column
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:igDP="http://infragistics.com/DataPresenter"
xmlns:local="clr-namespace:ExternalSummaryCalculation"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="800">
<igDP:XamDataGrid DataSource="{Binding Items}">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings SummaryDisplayArea="BottomFixed" AllowSummaries="False" SummaryUIType="SingleSelect"/>
</igDP:XamDataGrid.FieldSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout Key="Parent">
<igDP:TextField Label="Description" Name="Description"/>
<igDP:NumericField Name="ChildTotalValue" Visibility="Collapsed"/>
<igDP:Field Name="Children"/>
<igDP:FieldLayout.SummaryDefinitions>
<igDP:SummaryDefinition SourceFieldName="ChildTotalValue" Calculator="Sum" PositionFieldName="Description" />
</igDP:FieldLayout.SummaryDefinitions>
</igDP:FieldLayout>
<igDP:FieldLayout Key="Child" ParentFieldName="Children" ParentFieldLayoutKey="Parent">
<igDP:NumericField Name="Value"/>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Window>
The MainWindow.xaml.vb code except that changes to cChild.value (as the user edits) need to cause INotifyPropertyChanged to be raised for the parent's TotalChildValue property. One way to do this is to hold a reference to the parent in the child. This is not considered good practice but is pragmatic. Another option would be to have the MainWindow implement a method that can do this via reflection but that would over-complicate this example.
public partial class MainWindow : Window
{
public class cParent : INotifyPropertyChanged
{
public string Description { get; set; }
get { return Children.Sum(c => c.Value); }
public List<cChild> Children { get; set; } = new List<cChild>();
if (PropertyChanged != null)
public cParent AddChild(decimal value)
Children.Add(new cChild(this, value));
}
{
public cChild(cParent parent, decimal value)
this.parent = parent;
get { return _Value; }
{
_Value = value;
}
new cParent() {Description="Parent 1" }.AddChild(1).AddChild(2),
InitializeComponent();
}
}
}
If you modify one of the values you can see the total being recalculated via the line parent.NotifyPropertyChanged("ChildTotalValue");
No comments:
Post a Comment