是否可以将 IMultiValueConverter 用于项目列表?

发布于 2024-09-25 11:46:14 字数 455 浏览 1 评论 0原文

我将 EmployeeList 作为 Employee 对象的 observableCollection。

Employee 对象有 Salary。

我想在 XAML 中显示一些值,例如员工的平均工资,并且当将项目添加到列表中或在任何更新的项目中更改工资字段时,UI 字段应该自动更新。

这可以通过创建平均值属性并侦听列表中的集合 Changed 和 ProperyChanged 处理程序来实现。

但是,我确信应该有其他更好的方法来做到这一点。 (就像使用 AttachedProperties 或 IValueConverter/IMultiValueConverter 一样)

对此,我有以下问题。

  1. 是否可以将 IMultiValueConverter 用于项目列表/ObservableCollection?当项目添加到列表中以及特定属性更改时,应该调用转换器吗?

I have EmployeeList as a observableCollection of Employee Object.

The Employee object has Salary.

I want to display a few values like average Salary of the Employees in XAML, and the UI field should be automatically updated when an item is added to the List or When Salary field is changed in any of the items updated.

This can be achieved by creating a property for average and listening to collection Changed and ProperyChanged handlers in the list.

But, I am sure that there should be some other better way to do this. (Like using AttachedProperties or IValueConverter/IMultiValueConverter)

Regarding This, I have following questions.

  1. Is It possible to use IMultiValueConverter for a List/ObservableCollection of Items? The converter should be called when an Item Added to the list as well as when a particular property changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-10-02 11:46:14

使用属性绝对是可行的方法,尤其是从 MVVM 的角度来看。想想奥卡姆剃刀:基本上,最简单的解决方案通常是最好的。

这无疑是最干净的解决方案,因此也是最易于维护的。另外,它是最具可扩展性的(如果您愿意,您可以轻松地为不同的计算添加新属性)。

您需要做的就是创建只读属性,并在集合更改时使用该属性的名称调用 PropertyChanged(听起来您正在这样做)。

例如,这是一个“平均”属性:

public Double Average   
{
    get { return mMyCollection.Average(); }
}

void mMyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    PropertyChanged(this, new PropertyChangedEventArgs("Average"));
}

附加属性不起作用 - 它们用于在子元素中指定父元素的属性。

从理论上讲,ValueConverters 可以工作(尽管它们可能必须位于列表中的每个项目以及整个集合上),但您并没有真正转换任何内容,而是根据现有数据提供附加数据。为此,您需要使用各种模板,并且每当您需要更改任何内容时,您都需要再次使用它们。它很快就会变得复杂,而且没有任何好处。

Using a property is definitely the way to go, especially from an MVVM standpoint. Think Occam's Razor: basically, the simplest solution is usually the best.

It's certainly the cleanest solution, and thus most maintainable. Plus, it is the most expandable (you can easily add new properties for different calculations if you'd like).

All you need to do is create read-only properties, and call PropertyChanged with that property's name when the collection changes (which it sounds like you are doing).

For example, here's an "Average" property:

public Double Average   
{
    get { return mMyCollection.Average(); }
}

void mMyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    PropertyChanged(this, new PropertyChangedEventArgs("Average"));
}

Attached properties won't work - they are for specifying a parent's property in a child element.

ValueConverters would work, in theory (although they would probably have to be on each item in the list, as well as the entire collection), but you're not really converting anything, you are providing additional data based on existing data. To do that, you would need to muck around with all kinds of Templates, and any time you needed to change anything, you'll need to muck around with them again. It would get complex in a hurry, and for no benefit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文