简体   繁体   English

Silverlight DataGrid中列的不同默认排序顺序

[英]Different default sorting order for columns in Silverlight DataGrid

In my Silverlight application, I have a DataGrid that shows a list of projects. 在我的Silverlight应用程序中,我有一个显示项目列表的DataGrid It is data-bound to a custom ObservableCollection which implements ICollectionView to provide SortDescription 's. 它与自定义的ObservableCollection数据绑定,后者实现ICollectionView以提供SortDescription

When the user clicks on a header, the data is sorted by this column in ascending order. 当用户单击标题时,该列将按升序对数据进行排序。 Most users expect alphabetical sort (ASC) for column "Name", but for "Creation Date" it makes more sense to show latest entries first (DESC). 大多数用户期望“名称”列按字母顺序排序(ASC),但对于“创建日期”,更有意义的是首先显示最新条目(DESC)。

Is it possible to override the default sorting order for some of the columns? 是否可以覆盖某些列的默认排序顺序?

You've got two options here, that I can see. 我在这里有两个选择。 You can extend ObservableCollection with custom sort functions for the grid in question. 您可以使用有关网格的自定义排序功能扩展ObservableCollection。 You can see how to do that here . 您可以在此处查看如何执行此操作

Your other option involves customizing the grid with hyperlink buttons in the header. 您的另一个选择涉及使用标题中的超链接按钮自定义网格。 You can then capture the click and sort the underlying collection any way you want. 然后,您可以捕获点击并以任何所需的方式对基础集合进行排序。

UI CODE UI代码

<my:DataGrid.Columns>
    <my:DataGridTextColumn DisplayMemberBinding="{Binding Name}">
        <my:DataGridTextColumn.Header>
            <HyperlinkButton Content="Name" Tag="Name"
                Click="Sort_Click" TextDecorations="Underline"/>
        </my:DataGridTextColumn.Header>
    </my:DataGridTextColumn>
    <my:DataGridTextColumn DisplayMemberBinding="{Binding CreationDate}">
        <my:DataGridTextColumn.Header>
            <HyperlinkButton Content="CreationDate" Tag="CreationDate"
                Click="Sort_Click" TextDecorations="Underline"/>
        </my:DataGridTextColumn.Header>
    </my:DataGridTextColumn>
</my:DataGrid.Columns>

Click Handlder 单击处理机

private void Sort_Click(object sender, RoutedEventArgs e)
{
    HyperlinkButton button = sender as HyperlinkButton;
    //  Which property are we sorting?
    string sortProperty = button.Tag.ToString();
    //  sort direction (variable to keep track of which way were sorting last, add key checking an null checking)
    bool sortAsc = !this.columnSortState[sortProperty];
    IEnumerable<ITEMS> dataItems = dataGrid.ItemsSource as IEnumerable<ITEMS>;
    switch (sortProperty)
    {
        case "Name":
            dataItems = sortAsc
                ? dataItems.OrderBy(x => x.Name)
                : dataItems.OrderByDescending(x => x.Name);
            break;
        case "CreationDate":
            dataItems = sortAsc
                ? dataItems.OrderByDescending(x => x.CreationDate);
                : dataItems.OrderBy(x => x.CreationDate)
            break;
    }

    this.columnSortState[sortProperty] = isSortAsc;
}

I've taken out all the null checking and other stuff you should be doing in the Click Handler for brevity. 为了简洁起见,我已经删除了所有应该在Click Handler中执行的空检查和其他工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM