问题描述
我在Visual Studio 2008中使用C#,我已经安装了WPF Toolkit。我在testtest.xaml中创建了一个DataGrid。 ID和零件$列可以通过点击他们的尊重列标题来对DataGrid进行排序。但是,列标题Complete Date没有这种能力。我使用标签DataGridTemplateColumn格式化此列的日期。如何编辑列标题完成日期,以便您可以单击完整日期列标题并对该列进行排序。如果单击列,箭头不仅不显示,而且列标题不是可点击。谢谢
; Label Height =22HorizontalAlignment =LeftMargin =10,45,0,0Name =label1VerticalAlignment =TopWidth =41>任务< / Label>
< my:DataGrid ItemsSource ={Binding}AutoGenerateColumns =FalseMargin =140,83,67,28Name =dataGrid1GridLinesVisibility =VerticalIsReadOnly =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 =CompleteDateConverterCulture =en-GBStringFormat ={} {0:MM / dd / yyyy}/>
< /TextBlock.Text>
< / TextBlock>
< / DataTemplate>
< / my:DataGridTemplateColumn.CellTemplate>
< / my:DataGridTemplateColumn>
< / my:DataGrid.Columns>
< / my:DataGrid>
< / Grid>
在您的 DataGridTemplateColumn
中,您将SortMemberPath设置为。如果将其设置为项目上的实际属性(例如CompleteDate),则应该能够进行排序。您也可以在选定的列上设置 CanUserSort =true
或 CanUserSort =false
SortMemberPath给出当用户尝试排序时进行排序的属性。如果没有设置,那么网格不知道如何对列进行排序(它不是使用列中的文本)
< my:DataGridTemplateColumn SortMemberPath =CompleteDateHeader =Complete DateCanUserSort =true>
< my:DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock>
< TextBlock.Text>
< Binding Path =CompleteDateConverterCulture =en-GBStringFormat ={} {0:MM / dd / yyyy}/>
< /TextBlock.Text>
< / TextBlock>
< / DataTemplate>
< / my:DataGridTemplateColumn.CellTemplate>
< / my:DataGridTemplateColumn>
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
<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>
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.
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 not use the text in the column)
<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>
这篇关于使用C#在WPF中排序DataGrid列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!