2012-09-15 60 views
-2

你好我有一個observablecollection,允許用戶使用「添加」按鈕添加行。並且用戶可以在同一個標​​題中將具有相同名稱的項目分組。下面是代碼:WPF中的組總數

數據背後的代碼:

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代碼:

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); 
     } 

    } 
} 

我要添加一列:

<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> 

形式背後的代碼稱爲「數量」,它存儲添加到組中的每篇文章的數量。我的問題是:我如何獲得每個組的數量總和?

+0

解釋更多,* sum *的值是什麼,那並不代表任何事情。 – S3ddi9

+0

將相關代碼複製到此問題(以防萬一其他問題發生) –

+2

這不是一個真正的問題嗎?這可能不是一個很好的問題,但它肯定足夠具體,可以得到合理的回答。 – mydogisbox

回答

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); 
} 
+0

完全相同的代碼,我會刪除我的答案和您的代碼作品很好 – S3ddi9

+1

@SéddikLaraba - 我接受了你的編輯,但做了一個小小的改變:'dataGrid1.ItemsSource'不是'IEnumerable

'所以我保留myList從原來的代碼。 –

+0

@Erno非常感謝您的回答Erno,代碼工作得很好。 – valid90