如何在ObservableCollection 中更改属性时在ViewModel中触发OnPropertyChanged?(How to Trigger OnPropertyChanged in Vi

编程入门 行业动态 更新时间:2024-10-26 23:30:17
如何在ObservableCollection 中更改属性时在ViewModel中触发OnPropertyChanged?(How to Trigger OnPropertyChanged in ViewModel on change of Property inside ObservableCollection?)

我通常不会在StackOverflow上提问,但这个问题确实让我感到满意。

当ObservableCollection中的属性发生更改时,我需要为位于我的ViewModel中的属性触发OnPropertyChanged调用。 这是我的设置:

public sealed class ObjectExtended { private decimal? _percentage; public decimal? Percentage { get { return _percentage; } set { if (_percentage == value) return; _percentage = value; OnPropertyChanged("Percentage"); TODO: //Need to fire OnPropertyChanged("TotalCost") on ViewModel } } } public class TheViewModel : ViewModelBase { private ObservableCollection<ObjectExtended> _objects; public ObservableCollection<ObjectExtended> Objects { get { return _objects; } set { if (Equals(_objects, value)) return; _objects = value; OnPropertyChanged("Objects"); } } public decimal? TotalCost { get { var result = Objects.Where(x => x.status == SelectedObject.status).Select(x => x.costValue).Sum(); return Math.Truncate(result * 100) / 100; } } }

因此,当编辑百分比时,我想在视图中触发TotalCost值的更新。 有关如何做到这一点的任何建议?

I normally don't ask questions on StackOverflow but this problem is really doing my head in.

I have the need to fire a OnPropertyChanged-call for a property located in my ViewModel when a property inside ObservableCollection changes. Here is my set-up:

public sealed class ObjectExtended { private decimal? _percentage; public decimal? Percentage { get { return _percentage; } set { if (_percentage == value) return; _percentage = value; OnPropertyChanged("Percentage"); TODO: //Need to fire OnPropertyChanged("TotalCost") on ViewModel } } } public class TheViewModel : ViewModelBase { private ObservableCollection<ObjectExtended> _objects; public ObservableCollection<ObjectExtended> Objects { get { return _objects; } set { if (Equals(_objects, value)) return; _objects = value; OnPropertyChanged("Objects"); } } public decimal? TotalCost { get { var result = Objects.Where(x => x.status == SelectedObject.status).Select(x => x.costValue).Sum(); return Math.Truncate(result * 100) / 100; } } }

So when the Percentage is edited, I want to trigger an update of the value of TotalCost in the view. Any suggestions on how to do this?

最满意答案

一个非常简单的方法是只需挂钩列表项的属性更改事件,并在您正在查看的属性发生更改时将您自己的属性更改。

在创建可观察的集合之后

Objects.CollectionChanged += Objects_CollectionChanged;

然后

void Objects_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { switch (e.Action) { case System.Collections.Specialized.NotifyCollectionChangedAction.Add: foreach (var item in e.NewItems.Cast<System.ComponentModel.INotifyPropertyChanged>()) { item.PropertyChanged += item_PropertyChanged; } break; case System.Collections.Specialized.NotifyCollectionChangedAction.Move: break; case System.Collections.Specialized.NotifyCollectionChangedAction.Remove: foreach (var item in e.OldItems.Cast<System.ComponentModel.INotifyPropertyChanged>()) { item.PropertyChanged -= item_PropertyChanged; } break; case System.Collections.Specialized.NotifyCollectionChangedAction.Replace: break; case System.Collections.Specialized.NotifyCollectionChangedAction.Reset: break; default: break; } } void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if(e.PropertyName == "Percentage") { RaisePropertyChanged("TotalCost"); } }

您还需要处理重置(清除)并更换箱子以确保安全。

此外,如果您的可观察集合在附加集合更改处理程序之前有任何项目,那么您将不得不循环并挂钩它们。

A pretty simple way to do it is to just hook into the property changed events of the list items and raise your own property changed whenever the property you are watching for changes.

After your observable collection is created

Objects.CollectionChanged += Objects_CollectionChanged;

Then

void Objects_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { switch (e.Action) { case System.Collections.Specialized.NotifyCollectionChangedAction.Add: foreach (var item in e.NewItems.Cast<System.ComponentModel.INotifyPropertyChanged>()) { item.PropertyChanged += item_PropertyChanged; } break; case System.Collections.Specialized.NotifyCollectionChangedAction.Move: break; case System.Collections.Specialized.NotifyCollectionChangedAction.Remove: foreach (var item in e.OldItems.Cast<System.ComponentModel.INotifyPropertyChanged>()) { item.PropertyChanged -= item_PropertyChanged; } break; case System.Collections.Specialized.NotifyCollectionChangedAction.Replace: break; case System.Collections.Specialized.NotifyCollectionChangedAction.Reset: break; default: break; } } void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if(e.PropertyName == "Percentage") { RaisePropertyChanged("TotalCost"); } }

You will also need to handle the reset (clear) and replace cases to be safe.

Also if your observable collection has any items in it before attaching the collection changed handler then you will have to loop through and hook into them as well.

更多推荐

本文发布于:2023-08-03 15:56:00,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   如何在   ObservableCollection   ViewModel   Property

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!