WPF4 Datagrid 不按列标题排序 [英] WPF4 Datagrid doesn't sort on column headers

查看:31
本文介绍了WPF4 Datagrid 不按列标题排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MVVM 设计模式实现我的第一个 WPF 应用程序.

I am trying to implement my first WPF application using an MVVM design pattern.

我创建了一个数据绑定到 ObservableCollection<> 的应用程序.应用程序呈现良好,但我希望数据网格在我单击列标题时对行重新排序.

I created an application that is databinding to an ObservableCollection<>. The application renders fine, but I expect the datagrid to re-sort the rows when I click on the column headers.

我研究了以下帖子:数据源必须实现 IEnumerable,我的是.我需要设置 CanUserSortColumns 和/或 CanUserSort(在每一列上),我做到了.我可以通过指定排序回调函数来实现自定义排序,我做到了.

I've researched posts that said: The data source has to implement IEnumerable, mine does. I need to set CanUserSortColumns and/or CanUserSort (on each column), I did. I could implement a custom sort by specifying the Sorting callback function, I did.

这些似乎都没有帮助.在调试器中,我的 _customerGrid_Sorting 函数永远不会被调用,点击列标题也没有效果.

None of these seem to have helped. In the debugger, my _customerGrid_Sorting function never gets called and clicking on the column headers has no effect.

我是否遗漏了后面的 XAML 中的某些内容?我需要实现另一个接口吗?我现在感觉很失落,所以感谢任何帮助.

Am I missing something in the XAML that follows? Is there another interface I need to implement? I am feeling very lost right now, so any help is appreciated.

XAML:

        <DataGrid Name="_customerGrid" 
            AutoGenerateColumns="False" 
            CanUserSortColumns="True"
            ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
            HorizontalAlignment="Left" 
            ItemsSource="{Binding Path=AllCustomers}" 
            RowDetailsVisibilityMode="VisibleWhenSelected"
            RowStyle="{StaticResource DataGridRowStyle}"
            SelectionUnit="FullRow"
            Sorting="_customerGrid_Sorting"
            VerticalAlignment="Top">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Cust ID" MinWidth="90" CanUserSort="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=CustID}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Name" MinWidth="300" CanUserSort="True" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

推荐答案

问题是这段代码

<DataGridTemplateColumn Header="Cust ID" MinWidth="90" CanUserSort="True" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=CustID}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

默认排序仅适用于 DataGridTextColumn、DataGridComboBoxColumn 等.

现在这里的网格不知道如何排序,因为您已经在其中定义了一个 DataTemplate 和一个 Label.其他人可能在 DataTemplate 中定义了 SstackTemplate 等.因此 DataGrid 将不知道如何对其进行排序.

The default sorting works as stated everywhere only for DataGridTextColumn, DataGridComboBoxColumn,etc.

Now here the grid does not know how to sort because you have defined a DataTemplate and a Label inside it. Some other person might have defines a SstackTemplate, etc inside DataTemplate. So the DataGrid will not know how to sort it.

我会尽快向您发布示例代码,了解我们可以为此类 DataGridTemplateColumn 做些什么.但希望我给你的方向有所帮助!!

I will try to post you a sample code very soon as what we can do for such DataGridTemplateColumn. But hope the direction I have given you helps!!

<小时>似乎答案很简单.我使用 MVVM 使用示例 DataGrid 进行了尝试,但我觉得我的解决方案应该适合您

在下面的 DataGridTemplateColumn 中一切正常,但您缺少一个名为 SortMemberPath 的非常重要的属性.Thw WPF 应该知道它应该对哪个属性进行排序.

Everything is fine in the below DataGridTemplateColumn but you are missing a very important attribute called SortMemberPath. Thw WPF should know on which property it should sort.

你发的原帖是这样的

 <DataGridTemplateColumn Header="Name" MinWidth="300" CanUserSort="True" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=Name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>


我不知道你的数据结构.但是修改后的应该是这样的


I dont know your data structures. But the modified one should look like this

 <DataGridTemplateColumn  SortMemberPath="Name" Header="Name" MinWidth="300" CanUserSort="True" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=Name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>

希望对你有帮助!!如果它对您没有帮助,请告诉我,我会尝试将我的应用程序发布到某个博客中(尽管我很懒惰!!)

I hope it helps you!! Let me know if it does not help you, I will try to post my application in some blog ( I am lazy to do it though !!)

这篇关于WPF4 Datagrid 不按列标题排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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