如何从 DataGridColumnHeader 样式获取 WPF 列绑定字段
How to get the WPF column binding field from DataGridColumnHeader style
我正在为 WPF 数据网格进行自定义排序(因为我使用分页所以我不能使用默认排序),我如何才能获得该列绑定到的字段的名称?下面是我当前的代码 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
但它 return 为空?
我想通了,我给样式一个键
<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}"/>
我正在为 WPF 数据网格进行自定义排序(因为我使用分页所以我不能使用默认排序),我如何才能获得该列绑定到的字段的名称?下面是我当前的代码 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
但它 return 为空?
我想通了,我给样式一个键
<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}"/>