我正在为WPF datagrid做自定义排序(因为我使用分页因此我不能使用默认排序),如何获取列绑定到的字段的名称?以下是DataGrid.Resources
<Style TargetType="DataGridColumnHeader">
<Setter Property="Command" Value="{Binding Path=DataContext.SortCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
<Setter Property="CommandParameter" Value="{Binding Path=DisplayIndex, RelativeSource={RelativeSource Mode=Self}}"/>
</Style>
我尝试获取Column
但它返回null?
答案 0 :(得分:0)
我明白了,我给这个风格一把钥匙
<DataGrid.Resources>
<Style x:Key="SortableColumnHeader" TargetType="DataGridColumnHeader">
<Setter Property="Command" Value="{Binding Path=DataContext.SortCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
<Setter Property="CommandParameter" Value="{Binding Path=Column.Binding.Path.Path, RelativeSource={RelativeSource Mode=Self}}"/>
</Style>
</DataGrid.Resources>
并将其应用于列的HeaderStyle中并且有效
<DataGridTextColumn Binding="{Binding Name}" Header="Column Name" MinWidth="150" HeaderStyle="{StaticResource SortableColumnHeader}"/>