c# - 如何对 WPF DataGrid 中除第一列之外的所有列进行排序

标签 c# wpf datagrid

我有一个 WPF DataGrid,它的第一列包含一个表示“排名”或“位置”的数字。我想要的是,当我单击某一列以根据该列对表格进行排序时,第一列保持原样。

例如:

Position  Name   Games   Points    PPG
1         John   2       10        5
2         Mary   3       12        4

按游戏降序排序时应变为

Position  Name   Games   Points    PPG
1         Mary   3       12        4
2         John   2       10        5

有办法做到这一点吗? DataColumn 似乎没有任何此类属性,并且 FreezeColumnsCount 仅始终将它们保留在 View 中,但不会阻止它们与其余数据一起排序。

最佳答案

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ffd7037d-dd77-44ec-9011-d0d43d5706aa/我刚刚找到了一个关于我在评论中建议的示例(来自 Adbie 的回答):

<DataGrid LoadingRow="dg_LoadingRow">
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Header, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" />
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>


private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}


OP编辑:
我刚刚验证了即使没有绑定(bind)它也能工作。 cs代码很好,但在XAML中你只需要这样做

<DataGrid LoadingRow="dg_LoadingRow" />

关于c# - 如何对 WPF DataGrid 中除第一列之外的所有列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10321008/

相关文章:

wpf - 获取网格的中间行以填充可用空间

c# - 如何设置ComponentArt datagrid添加bootstrap Grid系统datagrid?

c# - Winforms 选项卡页面上的关闭按钮

wpf - 使用带有 ItemsControl 的 ScrollViewer 和 Canvas 作为 ItemsPanel

c# - 为什么将 int 从 SqlDataReader 转换为 long 会导致 InvalidCastException?

wpf - ListBox 中的所有 ComboBox 在其中任何一个更改时都会更改

c++ - 正确处理listview中可编辑子项的子项编辑(或取消子项编辑)

WPF - DataGrid 列标题对齐

c# - 泛型开放和封闭构造类型

c# - 单元测试删除,即使在数据库中删除了记录也返回