2010-09-17 70 views
2
排序

首先讓我澄清(如果標題是不夠清楚):我在討論從.NET 4.0框架System.Windows.Controls.DataGrid的工具包版本。WPF DataGrid中(在.NET 4.0中沒有工具包),頭與ICollectionView

我目前正在建立一個小的可重用的類/視圖模型/等在我的項目中建立一個相當強大的DataGrid爲我的應用程序。

現在,默認情況下給出IEnumarable<> ItemsSource DataGrid控件支持標題單擊列排序。

但是,我的實現現在使用CollectionViewSourceICollectionView將數據成員公開到DataGrid。當以這種方式聯繫起來時,它似乎依靠SortDescriptions進行排序。

雖然我喜歡從代碼控制(可以掛鉤各種各樣的東西),我還需要我的用戶能夠點擊標題欄來排序他們的結果。我正在尋找一種鉤住標題欄點擊的方式來發信號通知我的代碼,以適當調整CollectionViewSource

  1. 我將不得不重新設置標題按鈕樣式來觸發事件嗎?
  2. 有沒有辦法從DataGrid控件的現有頭掛鉤排序事件?
  3. 什麼是最好的方式去做這件事?
  4. 我是不是正確使用ICollectionView

回答

1

其實我找到了答案。我的一個完整的錯誤。它源於DataGrid控件中列的設置。

必須確保在DataGridColumn上設置了CanUserSort="True",並且設置了SortMemberPath屬性。

實施例:

  <DataGridTemplateColumn Header="Account #" Width="Auto" CanUserSort="True" SortMemberPath="AccountNum"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding AccountNum}" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
-1

您是否可以將ItemSource設置爲ObservableCollection <>?然後,當用戶用戶單擊列標題時,DataGrid將自動允許排序。

+0

不,我要求分組,並且可能在稍後的日期與ICollectionView接口濾波。 – Aren 2010-09-17 21:17:48

+0

綁定實現IList的集合,因此List和ObserrvableColleciton :) – 2010-09-17 21:43:30

+0

將CanUserSort設置爲true並在列上設置SortMemberPath是如何設置使用數據網格進行排序,而不是將網格綁定到什麼集合。 – 2011-02-07 05:23:36

4

您可以處理DataGrid.Sorting事件,使用其EventArgs的「e.Column」屬性獲取有關點擊列標題的信息,並設置「e.Handled = true」以防止DataGrid的默認排序引入。以下是一個演示這一個小例子:

XAML:

<Window x:Class="StackOverflow.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:StackOverflow" 
     Title="MainWindow" Height="350" Width="525"> 
    <DataGrid x:Name="DataGrid" Sorting="DataGrid_Sorting" CanUserAddRows="False"> 

     <DataGrid.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.HeaderTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Name}"/> 
        </DataTemplate> 
       </GroupStyle.HeaderTemplate> 
      </GroupStyle> 
     </DataGrid.GroupStyle> 

     <DataGrid.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}"/> 
      </DataTemplate> 
     </DataGrid.ItemTemplate> 
    </DataGrid> 
</Window> 

代碼隱藏:

using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.ComponentModel; 

namespace StackOverflow 
{ 
    public partial class MainWindow : Window 
    { 
     ListCollectionView lcv; 
     public MainWindow() 
     { 
      InitializeComponent(); 

      List<Item> items = new List<Item>(); 
      items.Add(new Item() { Name = "Item1", Category = "A" }); 

      items.Add(new Item() { Name = "Item5", Category = "B" }); 
      items.Add(new Item() { Name = "Item3", Category = "A" }); 
      items.Add(new Item() { Name = "Item4", Category = "B" }); 
      items.Add(new Item() { Name = "Item2", Category = "A" }); 

      lcv = new ListCollectionView(items); 
      lcv.GroupDescriptions.Add(new PropertyGroupDescription("Category")); 

      this.DataGrid.ItemsSource = lcv; 

     } 

     public class Item 
     { 
      public string Name { get; set; } 
      public string Category { get; set; } 
     } 

     private void DataGrid_Sorting(object sender, DataGridSortingEventArgs e) 
     {    
      e.Handled = true; 
      lcv.SortDescriptions.Add(new SortDescription(e.Column.SortMemberPath, ListSortDirection.Ascending)); 
     } 
    } 
}