计算ListView列的总和WPF C#

时间:2015-03-05 16:13:13

标签: c# wpf listview

我正在为一个酒吧创建一个EPOS系统,我的项目只是为了测试我的技能。

我遇到了问题,我已经设法将所有产品放在WrapPanel中,点击后我也设法让它们在ListView控件中显示。

但是,我似乎无法将总数显示在ListView下方的标签中,实际上,每次将产品添加到ListView时,我希望通过将所有价格中的所有价格相加来更新总数。价格“列并在下面的标签中显示。但我甚至无法通过按钮打印总数,更不用说自动执行了。

到目前为止,这是我的按钮代码。

不要建议SubItems,因为它在WPF中不起作用。

private void Button_Click_1(object sender, RoutedEventArgs e) {

     decimal total = 0;


     foreach (ListViewItem o in orderDetailsListView.Items)
     {
         total = total + (decimal)(orderDetailsListView.SelectedItems[1]);
     }  
     totalOutputLabel.Content = total;
}

2 个答案:

答案 0 :(得分:1)

我在下面回答了你的另一个问题,在你发布之前你删除了同一个程序。它涵盖了更新价格,但也涵盖了更多(使用已删除问题中的信息)。


首先,如果您希望在更新列表中的项目时更新屏幕,则必须使类实现INotifyPropertyChanged

public class OrderDetailsListItem : INotifyPropertyChanged
{
    private string _name;
    private decimal _price;
    private int _quantity;

    public string Name
    {
        get { return _name; }
        set
        {
            if (value == _name) return;
            _name = value;
            OnPropertyChanged();
        }
    }

    public decimal Price
    {
        get { return _price; }
        set
        {
            if (value == _price) return;
            _price = value;
            OnPropertyChanged();
        }
    }

    public int Quantity
    {
        get { return _quantity; }
        set
        {
            if (value == _quantity) return;
            _quantity = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

现在,当价格或数量发生变化时,它会让绑定知道该项目已被更改。

接下来,您if (OrderItem.Contains(导致重复项目显示的原因是您必须实施Equals((最好是GetHashCode())才能使Contains(工作。

public class OrderDetailsListItem : INotifyPropertyChanged, IEquatable<OrderDetailsListItem>
{
    //(Snip everything from the first example)

    public bool Equals(OrderDetailsListItem other)
    {
        if (ReferenceEquals(null, other)) return false;
        return string.Equals(_name, other._name);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as OrderDetailsListItem);
    }

    public override int GetHashCode()
    {
        return (_name != null ? _name.GetHashCode() : 0);
    }
}

另一点,在点击按钮时不要OrderItem.CollectionChanged +=,您将在每次收集更改事件时创建额外的事件调用。只需在构造函数中设置一个,这是您需要的唯一偶数订阅。但是,有一个更好的集合可供使用BindingList<T>及其ListChanged事件。 BindingList将在ObserveableCollection引发CollectionChanged的所有情况下引发ListChange事件,但此外,当集合中的任何项引发INotifyPropertyChanged事件时,它也会引发事件。

public MainWindow()
{
    _orderItem = new BindingList<OrderDetailsListItem>();
    _orderItem.ListChanged += OrderItemListChanged;
    InitializeComponent();
    GetBeerInfo();

    //You will see why all the the rest of the items were removed in the next part.
}

private void OrderItemListChanged(object sender, ListChangedEventArgs e)
{
    TotalPrice = OrderItem.Select(x => x.Price).Sum();
}

最后,我打赌你来自Winforms背景。 WPF基于绑定比winforms更多,我以前编写的代码很像你在我真正接受这一点之前所做的事情。所有这些标签和集合的赋值应该在XAML中完成,带有绑定,这允许用于像INotifyPropertyChanged事件一样自动更新屏幕而无需函数调用。

这是一个简单的程序设计,它运行并使用绑定和我谈到的所有其他内容。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myNamespace ="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <Button Content="{x:Static myNamespace:GlobalVariables._amstelProductName}" Click="amstelBeerButton_Click"/>


            <TextBlock Text="{Binding TotalPrice, StringFormat=Total: {0:c}}"/>
        </StackPanel>

        <ListView Grid.Column="1" ItemsSource="{Binding OrderItem}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=c}" Header="Price"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Quantity, StringFormat=N0}" Header="Quantity"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApplication2
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static readonly DependencyProperty TotalPriceProperty = DependencyProperty.Register(
            "TotalPrice", typeof (decimal), typeof (MainWindow), new PropertyMetadata(default(decimal)));

        private readonly BindingList<OrderDetailsListItem> _orderItem;

        public MainWindow()
        {
            _orderItem = new BindingList<OrderDetailsListItem>();
            _orderItem.ListChanged += OrderItemListChanged;
            InitializeComponent();
            DataContext = this;
            GetBeerInfo();
        }

        public BindingList<OrderDetailsListItem> OrderItem
        {
            get { return _orderItem; }
        }

        public decimal TotalPrice
        {
            get { return (decimal) GetValue(TotalPriceProperty); }
            set { SetValue(TotalPriceProperty, value); }
        }

        private void GetBeerInfo()
        {
            OrderItem.Add(new OrderDetailsListItem
            {
                Name = "Some other beer",
                Price = 2m,
                Quantity = 1
            });
        }

        private void OrderItemListChanged(object sender, ListChangedEventArgs e)
        {
            TotalPrice = _orderItem.Select(x => x.Price).Sum();
        }

        private void amstelBeerButton_Click(object sender, RoutedEventArgs e)
        {
            //This variable makes me suspicous, this probibly should be a property in the class. 
            var quantityItem = GlobalVariables.quantityChosen;

            if (quantityItem == 0)
            {
                quantityItem = 1;
            }

            var item = OrderItem.FirstOrDefault(i => i.Name == GlobalVariables._amstelProductName);

            if (item == null)
            {
                OrderItem.Add(new OrderDetailsListItem
                {
                    Name = GlobalVariables._amstelProductName,
                    Quantity = quantityItem,
                    Price = GlobalVariables._amstelPrice
                });
            }
            else if (item != null)
            {
                item.Quantity = item.Quantity + quantityItem;
                item.Price = item.Price*item.Quantity;
            }
            //The UpdatePrice function is nolonger needed now that it is a bound property.
        }
    }

    public class GlobalVariables
    {
        public static int quantityChosen = 0;
        public static string _amstelProductName = "Amstel Beer";
        public static decimal _amstelPrice = 5;
    }

    public class OrderDetailsListItem : INotifyPropertyChanged, IEquatable<OrderDetailsListItem>
    {
        private string _name;
        private decimal _price;
        private int _quantity;

        public string Name
        {
            get { return _name; }
            set
            {
                if (value == _name) return;
                _name = value;
                OnPropertyChanged();
            }
        }

        public decimal Price
        {
            get { return _price; }
            set
            {
                if (value == _price) return;
                _price = value;
                OnPropertyChanged();
            }
        }

        public int Quantity
        {
            get { return _quantity; }
            set
            {
                if (value == _quantity) return;
                _quantity = value;
                OnPropertyChanged();
            }
        }

        public bool Equals(OrderDetailsListItem other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return string.Equals(_name, other._name);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public override bool Equals(object obj)
        {
            return Equals(obj as OrderDetailsListItem);
        }

        public override int GetHashCode()
        {
            return (_name != null ? _name.GetHashCode() : 0);
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

答案 1 :(得分:0)

刚刚对此进行了测试,您应该确保通过添加断点进入事件处理程序。如果不是,请确保已将处理程序注册到click事件,例如:

<Button Name="TestButton" Click="Button_Click_1"/>

如果您正在使用WPF,我强烈建议您在某些时候查看MVVM和数据绑定。