[英]Calculating total of a ListView column WPF C#
我正在為自己的項目創建一個用於酒吧的EPOS系統,目的只是為了測試我的技能。
我遇到了一個問題,我設法將所有產品放在WrapPanel
並且單擊ive時也設法使它們顯示在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;
}
我在下面這個問題的答案上寫下了這個答案,這個問題是您在我可以發布之前刪除的同一程序的。 它涵蓋了更新價格,但還涵蓋了更多內容(使用已刪除問題中的信息)。
首先,如果要在更新列表中的項目時更新屏幕,則必須使該類實現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
事件。 在ObserveableCollection引發CollectionChanged的所有情況下,BindingList都會引發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));
}
}
}
剛剛對此進行了測試,您應該通過添加斷點來確保進入事件處理程序。 如果不是,請確保已將處理程序注冊到click事件,例如:
<Button Name="TestButton" Click="Button_Click_1"/>
如果您正在使用WPF,我強烈建議您在某個時候查看MVVM和數據綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.