Wpf 获取ObservableCollection列值之和

Wpf 获取ObservableCollection列值之和,wpf,Wpf,如何在更改数据网格列值时将ObservableCollection列值的总和获取到另一个属性,-wpf mvvm paten以下是一个解决方案: Xaml: 您想在网格中显示为列还是在底部显示为摘要?感谢您的回答,底部显示摘要,我想在底部文本块中显示特定网格列值的总和,这里使用的是ObservableCollection数据绑定。因此,在这种情况下,Item ViewModel是MainViewModel中的嵌套类,这很方便,因为它允许您从MyItem setter中为Sum触发Property

如何在更改数据网格列值时将ObservableCollection列值的总和获取到另一个属性,-wpf mvvm paten

以下是一个解决方案:

Xaml:


您想在网格中显示为列还是在底部显示为摘要?感谢您的回答,底部显示摘要,我想在底部文本块中显示特定网格列值的总和,这里使用的是ObservableCollection数据绑定。因此,在这种情况下,Item ViewModel是MainViewModel中的嵌套类,这很方便,因为它允许您从MyItem setter中为Sum触发PropertyChanged事件。如果容器中的项的ViewModel不是公开ObservableCollection的ViewModel的嵌套类,那么通常可以这样做吗?
<Window x:Class="Mvvm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="300" Width="400" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="0" 
                  ItemsSource="{Binding VM.MyList}" 
                  SelectedItem="{Binding VM.MyItem , Mode=TwoWay}"
                  />
        <TextBlock Grid.Row="1" Text="{Binding VM.Sum}"/>
    </Grid>
</Window>
public class MainViewModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

    public ObservableCollection<ItemViewModel> MyList { get; set; }

    ItemViewModel _myItem;
    public ItemViewModel MyItem
    {
        get
        {
            return _myItem;
        }
        set
        {
            _myItem = value;
            OnPropertyChanged("MyItem");
            OnPropertyChanged("Sum");
        }
    }

    public int Sum
    { 
        get
        {
            return MyList.Sum(a=>a.Amount);
        }
    }

    public MainViewModel()
    {
        MyList = new ObservableCollection<ItemViewModel>();

        MyList.Add(new ItemViewModel { Amount = 5});
        MyList.Add(new ItemViewModel { Amount = 6});
    }

}

public class ItemViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        int _amount;
        public int Amount
        {
            get
            {
                return _amount;
            }
            set
            {
                _amount = value;
                OnPropertyChanged("Amount");
            }
        }
        //Other properties like Id, transactionDate, ...

    }
    public MainViewModel VM { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        VM = new MainViewModel();
        DataContext = this;

    }