如何对 WPF 工具包 DataGrid 上的 DataGridTemplateColumn 进行排序?
我有一个带有一个 DataGridTemplateColumn 的 WPF 工具包 DataGrid。我已在网格属性中指定我希望所有列都可排序,但 DataGridTemplateColumn 不允许这样做。所有其他列都允许排序。我什至尝试明确将该列的 CanUserSort 设置为 true,但没有成功。是否可以对模板列进行排序?我提供了一个适用于所有列的自定义排序器,但标题不允许单击排序。
<Controls:DataGrid ItemsSource="{Binding Events}" AutoGenerateColumns="False"
CanUserSortColumns="True"
CanUserReorderColumns="False"
Sorting="DataGrid_Sorting"
x:Name="EventsGrid">
<Controls:DataGrid.Columns>
<Controls:DataGridTemplateColumn Header="Type" Width="42" CanUserResize="False">
<Controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding EventTypeImage, Mode=OneWay}" HorizontalAlignment="Center" Width="16"/>
</DataTemplate>
</Controls:DataGridTemplateColumn.CellTemplate>
</Controls:DataGridTemplateColumn>
<Controls:DataGridTextColumn Header="Source"
Binding="{Binding Source, Mode=OneWay}" />
<Controls:DataGridTextColumn Header="Details" MinWidth="175"
Binding="{Binding Details, Mode=OneWay}" />
<Controls:DataGridTextColumn Header="Timestamp" MinWidth="175"
Binding="{Binding Timestamp, Mode=OneWay}"
IsReadOnly="True"/>
</Controls:DataGrid.Columns>
</Controls:DataGrid>
I have a WPF Toolkit DataGrid with one DataGridTemplateColumn. I've specified in a grid attribute that I wish all columns to be sortable, but the DataGridTemplateColumn won't allow it. All other columns do allow sorting. I've even tried explicitly setting CanUserSort to true for that column, but no luck. Is it even possible to sort a template column? I've provided a custom sorter that works for all columns, but the header won't allow a sort click.
<Controls:DataGrid ItemsSource="{Binding Events}" AutoGenerateColumns="False"
CanUserSortColumns="True"
CanUserReorderColumns="False"
Sorting="DataGrid_Sorting"
x:Name="EventsGrid">
<Controls:DataGrid.Columns>
<Controls:DataGridTemplateColumn Header="Type" Width="42" CanUserResize="False">
<Controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding EventTypeImage, Mode=OneWay}" HorizontalAlignment="Center" Width="16"/>
</DataTemplate>
</Controls:DataGridTemplateColumn.CellTemplate>
</Controls:DataGridTemplateColumn>
<Controls:DataGridTextColumn Header="Source"
Binding="{Binding Source, Mode=OneWay}" />
<Controls:DataGridTextColumn Header="Details" MinWidth="175"
Binding="{Binding Details, Mode=OneWay}" />
<Controls:DataGridTextColumn Header="Timestamp" MinWidth="175"
Binding="{Binding Timestamp, Mode=OneWay}"
IsReadOnly="True"/>
</Controls:DataGrid.Columns>
</Controls:DataGrid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将
Controls:DataGridTemplateColumn
上的SortMemberPath
属性设置为要排序的 CLR 属性的名称。You need to set the
SortMemberPath
property onControls:DataGridTemplateColumn
to the name of the CLR property you want to sort on.将
SortMemberPath="Name"
添加到 DataGridTemplateColumn。就开始排序了。Added
SortMemberPath="Name"
to DataGridTemplateColumn. It started sorting.