代码之家  ›  专栏  ›  技术社区  ›  devuxer

WPF中DataGrid的预排序

  •  26
  • devuxer  · 技术社区  · 16 年前

    DataGrid 在WPF应用程序中,有几个列,包括一个名称列。如果用户切换到特定视图,我希望数据按名称进行预排序(我希望在名称标题中显示一个排序箭头,就像用户单击了该标题一样)。但是,我找不到实现这一目标的预期属性。我在找类似的东西 SortColumn , SortColumnIndex , SortDirection 等等。

    ?

    5 回复  |  直到 16 年前
        1
  •  42
  •   DLeh    12 年前

    假设您谈论的是WPF Toolkit DataGrid控件,您只需要设置 the CanUserSortColumns property 为true,然后设置 the SortMemberPath property

    <Window.Resources>
        <CollectionViewSource x:Key="MyItemsViewSource" Source="{Binding MyItems}">
            <CollectionViewSource.SortDescriptions>
               <scm:SortDescription PropertyName="MyPropertyName"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    
    <DataGrid ItemsSource="{StaticResource MyItemsViewSource}">
    
    </DataGrid>
    

    “scm”名称空间前缀映射到System。SortDescription类所在的ComponentModel。

    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    

    我认为有足够多的人从这篇文章中得到了帮助,这条被投票支持的评论应该包含在这个答案中:

    我必须用这个来让它工作:

    <DataGrid ItemsSource="{Binding Source={StaticResource MyItemsViewSource}}">
    
        2
  •  18
  •   Community Mohan Dere    8 年前

    我知道这是一篇旧文章,但除了Drew Marsh的回答之外,为了回应DanM关于列标题箭头未出现的问题……您需要将SortDirection属性添加到DataGridColumn中:

    <DataGridTextColumn Header="Name" Binding="{Binding Name}" SortDirection="Ascending" />
    

    我发布了一个关于这个的问题,几天后找到了答案:

    ColumnHeader arrows not reflected when sorting a DataGrid in XAML

        3
  •  4
  •   LPL user462990    14 年前

    当您看到Items Source不支持CollectionViewSource异常时,可以将DataGrid的DataContext设置为“MyItems ViewSource”,并将Items Source设置为{Binding},如下所示:

    <DataGrid DataContext="{StaticResource MyItemsViewSource}" ItemsSource="{Binding}">
    </DataGrid>
    
        4
  •  2
  •   Václav Dajbych    16 年前

    当你看到 ItemsSource doesn't support CollectionViewSource 例外情况下,您可以在将集合引用到DataGrid之前按Linq对其进行排序:

    ObservableCollection<MyDataClass> myCollection = new ObservableCollection<MyDataClass>();
    dataGrid.ItemsSource = from item in myCollection orderby item select item;
    

    你必须实施 IComparable 接口 MyDataClass :

    public class MyDataClass : IComparable<MyDataClass> {
        public int CompareTo(Classified other) {
            return other.Value.CompareTo(this.Value); // DESC
            return this.Value.CompareTo(other.Value); // ASC
        }
    }
    
        5
  •  0
  •   Pramod Pawar    8 年前

    这对我有效。

    ListSortDirection sortDirection;
    int selectedColumnIndex;
    private void customerDataGrid_Sorting(object sender, DataGridSortingEventArgs e)
    {
        selectedColumnIndex = e.Column.DisplayIndex;
        sortDirection = (e.Column.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending: ListSortDirection.Ascending);
    }
    
    private void applySortDescriptions(ListSortDirection listSortDirection)
    {
        //Clear current sort descriptions 
        customerDataGrid.Items.SortDescriptions.Clear();
    
        //Get property name to apply sort based on desired column 
        string propertyName = customerDataGrid.Columns[selectedColumnIndex].SortMemberPath;
    
        //Add the new sort description 
        customerDataGrid.Items.SortDescriptions.Add(new SortDescription(propertyName, listSortDirection));
    
        //apply sort 
        applySortDirection(listSortDirection);
    
        //refresh items to display sort 
        customerDataGrid.Items.Refresh();
    }
    
    private void applySortDirection(ListSortDirection listSortDirection)
    {
        customerDataGrid.Columns[selectedColumnIndex].SortDirection = listSortDirection;
    }
    
    推荐文章