根据WPF中的项目列表计算总和,平均值 [英] Calculate sum , average based on list of items in WPF

查看:74
本文介绍了根据WPF中的项目列表计算总和,平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,我要根据提供的项目列表显示总和,平均值.

In WPF, I want to display sum, average values based on the List of Items provided.

例如,我有一个具有Salary属性的Employee对象,并且我想根据员工列表计算总工资.另外,employee对象是绑定到Items控件的数据,在该控件中将编辑薪水/新的Employee可能会添加到列表中.

For example, I have an Employee object with Salary Property, and I want to calculate total salary based on the employee list. Also, the employee object is data bound to a Items control where the Salary will be edited/new Employee may be added to the list.

有人可以给我提供解决方案吗?

Can anyone provide me a solution to achieve this?

推荐答案

我会这样做:

在您的代码后面或ViewModel(如果您使用MVVM)中,创建一个新的依赖项属性,称为AverageSalary.

In your code behind or ViewModel (if your using MVVM) create a new dependency property called AverageSalary.

然后将雇员的薪金也更改为依赖项属性.然后,在后面的代码中,您可以侦听Employee.Salary的更改时间. (有关详细信息,请参见此链接),并在其变化时重新计算如上所列的平均值,然后将其分配给您创建的AverageSalary依赖项属性.

Then change Salary on the employee to also be a dependency property. In the code behind you can then listen to when the Employee.Salary changes. (see this link for details on that) And when it changes recompute the average like listed above then assign it to the AverageSalary dependency property you created.

然后将您的UI绑定到AverageSalary属性,并且该属性应随员工薪水的变化而更新.

Then just bind your UI to the AverageSalary property and it should update as the employee's salaries change.

这篇关于根据WPF中的项目列表计算总和,平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆