DataGrid - 显示从网格顶部到当前行的运行总计

发布于 2024-09-08 09:44:50 字数 318 浏览 2 评论 0原文

如何显示 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 技术交流群。

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

发布评论

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

评论(6

神妖 2024-09-15 09:44:50

这只是一个想法:实际上,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.

别在捏我脸啦 2024-09-15 09:44:50

我不知道有推荐/支持/简单的方法来做到这一点。我能想到的选择是:
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

心碎的声音 2024-09-15 09:44:50

如果您不介意 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.

网白 2024-09-15 09:44:50

我最终通过扩展 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

浮生未歇 2024-09-15 09:44:50

查看此答案,了解如何将行计数列添加到列表框并使用值转换器进行填充。不要计算项目,而是将项目的总和相加。如果您需要虚拟机中的总和,这当然不是一个好的解决方案。

编号列表框

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

尾戒 2024-09-15 09:44:50
long cumulative = 0;
List<int> Values = GetValues();
Values.ForEach(v => v.RunningTotal = cumulative = cumulative + v.Value);

您只需对整个列表运行foreach,并通过公式获取运行总计
(运行总计 = 累计 = 累计 + 值);
我们需要不断保存之前的值。

long cumulative = 0;
List<int> Values = GetValues();
Values.ForEach(v => v.RunningTotal = cumulative = cumulative + v.Value);

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.

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