This is a migrated thread and some comments may be shown as answers.

Wpf RadGridView Aggregate Functions Sum Function not updated when change property value in other control

1 Answer 687 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Muhammad
Top achievements
Rank 1
Muhammad asked on 09 Feb 2018, 02:57 AM

1. Add items (click add new item).
2. Select item (select grid view row).
3. Change Quantity or Unit Price in grid view cell, the sum quantity and line total updated.
4. Change Quantity or Unit Price in other control (textbox), the sum quantity and line total not updated.
5. Add New Item or delete an Item. The sum quantiy and line total updated.

How can I fix the change quantity or unit price in other controll will update the sum quantity and line total?

Thanks

Xaml:

<Window
        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:local="clr-namespace:RadGridView_Bugs"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="RadGridView_Bugs.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <telerik:RadGridView Name="_gridView" AutoGenerateColumns="False" ShowColumnFooters="True">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Quantity" DataMemberBinding="{Binding Path=Quantity}">
                    <telerik:GridViewDataColumn.AggregateFunctions>
                        <telerik:SumFunction SourceField="Quantity" Caption="Quantity Total: "/>
                    </telerik:GridViewDataColumn.AggregateFunctions>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Unit Price" DataMemberBinding="{Binding Path=UnitPrice}"/>
                <telerik:GridViewDataColumn Header="Line Total" DataMemberBinding="{Binding Path=LineTotal}">
                    <telerik:GridViewDataColumn.AggregateFunctions>
                        <telerik:SumFunction SourceField="LineTotal" ResultFormatString=" {0:N0}" Caption="Line Total: "/>
                    </telerik:GridViewDataColumn.AggregateFunctions>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
        <StackPanel Grid.Column="1">
            <StackPanel Orientation="Horizontal">
                <Label Content="Quantity"/>
                <TextBox Name="_quantity" MinWidth="100" Text="{Binding ElementName=_gridView, Path=SelectedItem.Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="Unit Price"/>
                <TextBox Name="_unitPrice" MinWidth="100" Text="{Binding ElementName=_gridView, Path=SelectedItem.UnitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="Line Total"/>
                <TextBlock MinWidth="100" Text="{Binding ElementName=_gridView, Path=SelectedItem.LineTotal, Mode=OneWay}"/>
            </StackPanel>
            <Button Content="Add New Item" Name="addNewItem" Click="addNewItem_Click"/>
        </StackPanel>
       
    </Grid>
</Window>

 

Code behind:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RadGridView_Bugs
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void addNewItem_Click(object sender, RoutedEventArgs e)
        {
            _gridView.Items.Add(new LineItem() { Quantity = 1, UnitPrice = 100 });
        }
    }
    public sealed class LineItem : INotifyPropertyChanged
    {
        private decimal _quantity;
        private decimal _unitPrice;
        public LineItem()
        {
        }
        /// <summary>
        /// Gets or sets Quantity.
        /// </summary>
        public decimal Quantity
        {
            get { return _quantity; }
            set
            {
                if (value != _quantity)
                {
                    _quantity = value;
                    this.OnPropertyChanged("LineTotal");
                    this.OnPropertyChanged("Quantity");
                }
            }
        }
        /// <summary>
        /// Gets or sets UnitPrice.
        /// </summary>
        public decimal UnitPrice
        {
            get { return _unitPrice; }
            set
            {
                if (value != _unitPrice)
                {
                    _unitPrice = value;
                    this.OnPropertyChanged("LineTotal");
                    this.OnPropertyChanged("UnitPrice");
                }
            }
        }
        public decimal LineTotal => this.Quantity * this.UnitPrice;
        private void OnPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}


1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 13 Feb 2018, 02:59 PM
Hello Muhammad,

Thank you for the provided code snippet.

May I kindly ask you to review the Aggregates are not updated help article, as this topic is discussed in details in it?

Hopefully, it helps.

Regards,
Stefan
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Muhammad
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or