DataGrid - 显示从网格顶部到当前行的运行总计
如何显示 DataGrid 中显示的每条记录的特定列的运行总计?我想将计算逻辑保留在视图模型中。
期望的输出:
Value | Running Total 25 25 10 35 -2 33
谢谢,
Ben
编辑:请记住,当发生用户触发的 DataGrid 排序时,必须重新计算运行总计,因为记录显示顺序已更改。
我真的很想将其逻辑放在视图模型中,而不是 WPF 代码中(例如不使用 IValueConverter)。
How would I display a running total for a particular column of each record that is displayed in a DataGrid? I'd like to keep the computation logic in the view model.
Desired Output:
Value | Running Total 25 25 10 35 -2 33
Thank you,
Ben
Edit: Keep in mind that when a user-triggered DataGrid sort occurs, the running totals must be recalculated because the record display order has changed.
I'd really like to put the logic for this in the view model, not in WPF code (e.g. not using an IValueConverter).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这只是一个想法:实际上,DataGrid 使用 ICollectionView 接口( http: //msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx)来实现排序(如 http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid( VS.95).aspx 在分组、排序和过滤部分
因此,您可以创建实现 ICollectionView 接口的类,并观察排序以及相应的运行总计更新。
It is only idea: actually, DataGrid uses ICollectionView interface ( http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx) to implement sorting (as described in http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(VS.95).aspx at Grouping, Sorting, and Filtering section.
So, you can create class that implements ICollectionView interface, and observe sorting with corresponding updating of running total.
我不知道有推荐/支持/简单的方法来做到这一点。我能想到的选择是:
1)在数据网格之外有一些 ui 元素来保存总数。这很容易绑定到您的虚拟机,但解决将出现的对齐问题有点困难,因为您可能希望总数就在列下方。这就是我推荐的。
2)在网格中有一个虚拟的最后一行;这解决了对齐问题,但给虚拟机添加了一些老套的逻辑,而且可能看起来不太好。
HTH,
贝里尔
I don't know there is a recommended / supported / simple way to do this. The choices I can think of are:
1) Have some ui element outside of the data grid to hold the total. This is easy to bind to your VM but a bit of a pain to work out the alignment issues that will come up, as you likely want the total right under the column. This is what I would recommend.
2) Have a dummy last row in the grid; this solves the alignment problem but adds hacky logic to the VM, and probably doesn't look so great on top of it.
HTH,
Berryl
如果您不介意 VB 或 C# 的答案,则本教程显示如何在 VB 中执行您想要的操作以及本教程 在 C# 中显示它。两者都在网格中使用虚拟列。
If you don't mind the answer in VB or C# then this tutorial shows how to do what you want in VB and this tutorial shows it in C#. Both use a dummy column in the grid.
我最终通过扩展 DataGrid 以公开一个属性来解决这个问题,该属性可以绑定到视图模型上的“度假村已发生”回调。
详细信息: http://bengribaudo.com/博客/2010/07/14/3/datagrid-per-row-running-totals
I ended up solving this problem by extending DataGrid to expose a property which could be bound to a "resort has occured" callback on the view model.
Details: http://bengribaudo.com/blog/2010/07/14/3/datagrid-per-row-running-totals
查看此答案,了解如何将行计数列添加到列表框并使用值转换器进行填充。不要计算项目,而是将项目的总和相加。如果您需要虚拟机中的总和,这当然不是一个好的解决方案。
编号列表框
Check out this answer for how to add a row count column to a ListBox and populate using a value converter. Instead of counting item, add up the sum of items. If you need the total sum in your VM this is of course not a good solution.
Numbered listbox
您只需对整个列表运行foreach,并通过公式获取运行总计
(运行总计 = 累计 = 累计 + 值);
我们需要不断保存之前的值。
You can just run foreach for the whole list and get your running total by the formula
(RunningTotal = cumulative = cumulative + Value);
We need to continously save the previous value.