WPF中的组总和

时间:2012-09-15 19:26:43

标签: c# wpf grouping observablecollection

您好我有一个observablecollection,允许用户使用“添加”按钮添加行。并且用户可以在同一标题内对具有相同名称的项目进行分组。这是代码:

数据背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfDataGridWithDataTable
{
    public class Article
    {
        public  Article ()
        {

        }
        private int  _modelNumber;
        public int ModelNumber
        {
            get { return _modelNumber; }
            set { _modelNumber = value; OnPropertyChanged("ModelNumber"); }
        }

        private string _modelName;
        public string ModelName
        {
            get { return _modelName; }
            set { _modelName = value; OnPropertyChanged("ModelName"); }
        }

        private decimal  _unitCost;
        public decimal UnitCost
        {
            get { return _unitCost; }
            set { _unitCost = value; OnPropertyChanged("UnitCost"); }
        }

        private string  _description ;
        public string Description
        {
            get { return _description; }
            set { _description = value; OnPropertyChanged("Description"); }
        }


        #region INotifyPropertyChanged Membres

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion
    }
    public class ListArticles : ObservableCollection<Article > 
    {
        public Article a;
        public ListArticles()
        {

                a = new Article();
                this.Add(a);

        }

    }

}

XAML代码:

<Window x:Class="WpfDataGridWithDataTable.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfDataGridWithDataTable"
        Title="Window1" Height="300" Width="300">
    <Grid
        Name="gridPanel">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <DataGrid 
            Grid.Column="0"
            Name="dataGrid1"  
            AutoGenerateColumns="True"  
            CanUserAddRows="True" 
            CanUserDeleteRows="True"
            CanUserResizeColumns="True"
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}"/>
        <ListBox 
            Grid.Column="1"
            Name="listBox1" 
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Article}">
                    <StackPanel    
                        Orientation="Horizontal">
                        <TextBlock 
                            Width="100"
                            Margin="10"  
                            Background="DarkBlue"
                            Foreground="White"
                            FontSize="14"
                            Text="{Binding ModelNumber}"/>
                        <TextBlock 
                            Width="100"
                            Margin="10" 
                            Background="DarkBlue"
                            Foreground="White"
                            FontSize="14"
                            Text="{Binding ModelName}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
            <Button 
                Grid.Row="1"
                Grid.Column="0"
                HorizontalAlignment="Left"
                Width="100"
                Name="btnAdd"
                Content="Add Item"
                Click="btnAdd_Click">
            </Button>
            <Button
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Right"
                Width="100"
                Name="btnDelete"
                Content="Delete Item"
                Click="btnDelete_Click" >
            </Button>
    </Grid>
</Window>

表单背后的代码:

namespace WpfDataGridWithDataTable
{

    public partial class Window1 : Window
    {
        private ListArticles myList;
        public Window1()
        {
            InitializeComponent();
            myList = new ListArticles();


            this.DataContext = myList;

        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            myList.Add(new Article());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            myList.Remove(this.dataGrid1.SelectedItem as Article);
        }

    }
}

我想添加一个名为“Quantity”的列,该列存储添加到组中的每篇文章的数量。我的问题是:我如何获得每组的数量总和?

1 个答案:

答案 0 :(得分:1)

使用Linq可以解决这个问题。 (下面的代码中可能有错误,我没有通过编译器传递它)

var groupSumsQuery = from model in myList
                     group model by model.ModelName into modelGroup
                     select new 
                         {
                             Name = modelGroup.Key,
                             Sum = modelGroup.Sum(model=>model.UnitCost)
                         };

foreach(var group in groupSumsQuery)
{
    Console.WriteLine("Total price for all {0}: {1}", group.Name, group.Sum);
}