Making a DataGrid Column Header sortable in WPF using C#

你。 提交于 2019-12-18 13:54:21

问题


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>


回答1:


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>


来源:https://stackoverflow.com/questions/1296532/making-a-datagrid-column-header-sortable-in-wpf-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!