Проверьте это: https://www.codeproject.com/Tips/694370/How-to-Listen-to-Property-Chang
Что вам нужно, так это уведомление о любых изменениях в Коллекции.
Обновление:
Скажите, данные, которые появятся в сетке:
public class Data
{
public string Name { get; set; }
private int _Salary;
public int Salary
{
get
{
return _Salary;
}
set
{
_Salary = value;
SalaryChanged?.Invoke();
}
}
public Action SalaryChanged { get; set; }
}
Вместо ObservableCollection - используйте это:
public class ObsCollection : ObservableCollection<Data>
{
private Action SalaryChanged;
public ObsCollection(Action NotifyPropertyChanged)
{
SalaryChanged = NotifyPropertyChanged;
}
protected override void InsertItem(int index, Data item)
{
item.SalaryChanged = SalaryChanged;
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
base.RemoveItem(index);
SalaryChanged?.Invoke();
}
protected override void ClearItems()
{
base.ClearItems();
SalaryChanged?.Invoke();
}
}
Наконец - в модели View
public class MainWindowVM : INotifyPropertyChanged
{
private ObsCollection _Data = null;
public ObsCollection Data
{
get
{
return _Data;
}
set
{
_Data = value;
}
}
public int TotalSalary
{
get
{
return _Data.Sum(d => d.Salary);
}
}
public MainWindowVM()
{
_Data = new ObsCollection(NotifyTotalSalaryChanged);
LoadData();
}
public void LoadData()
{
_Data.Add(new Data { Name = "Test1", Salary = 1000 });
_Data.Add(new Data { Name = "Test2", Salary = 2000 });
_Data.Add(new Data { Name = "Test3", Salary = 3000 });
Notify("Data");
}
private void NotifyTotalSalaryChanged()
{
Notify("TotalSalary");
}
#region Property Changed Stuff
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
=============================================
Xaml просто:
<Window x:Class="CstomObserve.MainWindow"
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:CstomObserve"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainWindowVM/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ItemsSource="{Binding Data, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="1" Text="{Binding TotalSalary, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>