使用 C# 在 WPF 中使 DataGrid 列标题可排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1296532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a DataGrid Column Header sortable in WPF using C#
提问by Philip Rieck
I am using C# in Visual Studio 2008 and I have install the WPF Toolkit. I created a DataGrid in testtest.xaml. The ID and Parts $ columns have the ability to sort the DataGrid by clicking on their respecteive column headers. However, the column header Complete Date does not have that ability. I used the tag "DataGridTemplateColumn" to format the dates for this column. How do you program the column header Complete Date so you can click on the Complete Date column header and sort that column. If you click on the column, the arrow is not only not displayed but the column header is not "clickable". Thank you
我在 Visual Studio 2008 中使用 C#,并且安装了 WPF Toolkit。我在 testtest.xaml 中创建了一个 DataGrid。ID 和 Parts $ 列可以通过单击各自的列标题对 DataGrid 进行排序。但是,列标题完成日期没有这种能力。我使用标签“DataGridTemplateColumn”来格式化此列的日期。您如何对列标题完成日期进行编程,以便您可以单击完成日期列标题并对该列进行排序。如果单击该列,不仅不会显示箭头,而且列标题也无法“单击”。谢谢
<Label Height="22" HorizontalAlignment="Left" Margin="10,45,0,0" Name="label1" VerticalAlignment="Top" Width="41">Task</Label>
<my:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="140,83,67,28" Name="dataGrid1" GridLinesVisibility="Vertical" IsReadOnly="True">
<my:DataGrid.Columns>
<my:DataGridTextColumn Binding="{Binding Path=[ID]}" Header="ID" />
<my:DataGridTextColumn Binding="{Binding Path=p}" Header="Parts $" />
<my:DataGridTemplateColumn SortMemberPath="" Header="Complete Date">
<my:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<Binding Path="CompleteDate" ConverterCulture="en-GB" StringFormat="{}{0:MM/dd/yyyy}"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
回答by Philip Rieck
In your DataGridTemplateColumn
you have SortMemberPath set to "". If you set this to an actual property on the item (say, CompleteDate), you should be able to sort. You can also set CanUserSort="true"
or CanUserSort="false"
on selected columns.
在您的DataGridTemplateColumn
SortMemberPath 设置为“”。如果您将其设置为项目的实际属性(例如,CompleteDate),您应该能够进行排序。您还可以在选定的列上设置CanUserSort="true"
或CanUserSort="false"
。
SortMemberPath gives the property to sort on when the user attempts a sort. If this isn't set, then the grid doesn't know how to sort that column ( it does notuse the text in the column)
SortMemberPath 提供在用户尝试排序时进行排序的属性。如果没有设置,则网格不知道如何对该列进行排序(它并没有使用列文)
<my:DataGridTemplateColumn SortMemberPath="CompleteDate" Header="Complete Date" CanUserSort="true">
<my:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<Binding Path="CompleteDate" ConverterCulture="en-GB" StringFormat="{}{0:MM/dd/yyyy}"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>