使用 C# 在 WPF 中使 DataGrid 列标题可排序 [英] Making a DataGrid Column Header sortable in WPF using C#

查看:24
本文介绍了使用 C# 在 WPF 中使 DataGrid 列标题可排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2008 中使用 C#,并且安装了 WPF Toolkit.我在 testtest.xaml 中创建了一个 DataGrid.ID 和 Parts $ 列可以通过单击各自的列标题对 DataGrid 进行排序.但是,列标题完成日期没有这种能力.我使用标签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>

推荐答案

在您的 DataGridTemplateColumn 中,您将 SortMemberPath 设置为 "".如果您将此设置为项目的实际属性(例如,CompleteDate),您应该能够进行排序.您还可以在选定的列上设置 CanUserSort="true"CanUserSort="false".

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 提供在用户尝试排序时进行排序的属性.如果未设置,则网格不知道如何对该列进行排序(它使用该列中的文本)

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 列标题可排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆