在listview WPF中计算总计 [英] Calculating total in a listview WPF

查看:122
本文介绍了在listview WPF中计算总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个列表视图,我想在列表视图中添加所有小数,并在标签中显示总数。



理想情况下我希望每次更新列表视图时更新总数(在标签中)。但是现在我只想在标签上显示一个工作总计。



由于我在WPF工作,SubItems无法工作,我试图寻找解决方案在它周围,但我完全不知所措。



我试过这个。



So, i have a list view, and i want to add all of the decimals inside of the listview and display the total in a label.

Ideally i want the total (in the label) to be updated each time the listview is updated. But right now i just want a working total to be displayed in the label.

Since im working in WPF the SubItems doesnt work, i've tried looking for the solutions around it but i get completely overwhelmed by what i find.

I've tried this.

private void Button_Click_1(object sender, RoutedEventArgs e)
{

    decimal total = 0;

    foreach (ListViewItem lstViewItem in orderDetailsListView.Items)
    {

        foreach (ListViewItem o in orderDetailsListView.Items)
            total = total + Convert.ToDecimal(orderDetailsListView.Items.["Price"].Text);

        totalOutputLabel.Content = total;
    }
}







但它不起作用,因为标识符是预计在这里...项目。 [价格]





我知道它很简单,可以很快就会完成,但我搜索和搜索,我遇到的每个解决方案,无论是VB,winforms,数据网格还是我找到解决方案的难得机会,它不起作用,因为它们的部分代码不存在与我的一样。




But it doesnt work, because an identifier is expected here...Items.["Price"]


I know its so simple and can be done in no time, but i've searched and search and every solution i have come across, is either VB, winforms, datagrids and the rare chance i find a solution for mine, it doesnt work due to parts of their code not being the same as mine.

推荐答案

我的建议是创建一个从wpf ListView派生的CustomListView控件,并覆盖OnItemsChanged受保护的方法。



https://msdn.microsoft.com/en-us/library/system.windows.controls.listview.onitemschanged(v = vs.110).aspx [ ^ ]



在这里你可以验证NotifyCollectionChangedEventArgs Action属性以查看是否添加了一个项目,删除,替换或重置并添加逻辑以计算总数(如有必要)。



https://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedaction(v = VS。 110).aspx [ ^ ]
My suggestion would be to create a CustomListView control that is derived from the wpf ListView and override the OnItemsChanged protected method.

https://msdn.microsoft.com/en-us/library/system.windows.controls.listview.onitemschanged(v=vs.110).aspx[^]

Here you can verify NotifyCollectionChangedEventArgs Action property to see if a item was added, removed, replaced or reset and add the logic for calculating the total if necessary.

https://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedaction(v=vs.110).aspx[^]


这篇关于在listview WPF中计算总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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