我有一个 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/