Datagrid 排序(仍然)不适用于 Entity Framework 4.0
我在 VS2010 中编写的应用程序遇到问题。我需要在数据网格上显示数据绑定数据并允许用户对其内容进行排序。
这是我的情况:
我构建了一个非常简单的 SQL 数据库,其中有 1 个名为“Question”的表。该表有 2 列:“id_Question”和“QuestionText”。
我从此数据库创建了一个实体模型。
在“数据源”下,我将“问题”实体作为数据网格拖放到我的 WPF 窗口中。运行时,数据网格通常会填充数据库中的任何内容。太棒了。
我已将 DataGrid 和 DataGrid 列的“CanUserSortColumns”和“CanUserSort”属性设置为 True。单击列标题时,没有任何反应,也没有任何排序。
以下是 XAML 片段:
<DataGrid AutoGenerateColumns="False"
EnableRowVirtualization="True" Height="200"
HorizontalAlignment="Left" ItemsSource="{Binding}"
Margin="871,126,0,0" Name="questionsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top" Width="400"
CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="id_QuestionColumn1" Header="id Question" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=id_Question}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="questionTextColumn1"
Binding="{Binding Path=QuestionText}" Header="Question Text" Width="300"
CanUserSort="True"/>
</DataGrid.Columns>
</DataGrid>
我已在 Stack Overflow 和其他网站上阅读了以下许多其他相关文章/问题:
<一个href="https://stackoverflow.com/questions/4463505/how-to-sort-datagridview-when-bound-to-a-binding-source-that-is-linked-to-an-ef4">如何当绑定到链接到 EF4 实体的绑定源时对 DataGridView 进行排序
每个都提供了有用的见解,但我无法让它解决我的问题。我需要在代码隐藏文件中编写什么才能使其正常工作?
谢谢
I'm having trouble with an application I'm writing in VS2010. I need to display databound data on a Datagrid and allow the user to sort its contents.
This is my situation:
I've built a very simple SQL database with 1 table named "Question". The table has 2 columns, "id_Question" and "QuestionText".
I created an Entity model from this database.
Under "Data Sources", I've dragged and dropped the "Question" Entity as a Datagrid onto my WPF window. When run, the Datagrid populates normally with whatever is in the database. Great.
I've set the "CanUserSortColumns" and "CanUserSort" properties of the DataGrid and DataGrid columns to True. When the column header is clicked, nothing happens, nothing sorts.
Here is the XAML snippet:
<DataGrid AutoGenerateColumns="False"
EnableRowVirtualization="True" Height="200"
HorizontalAlignment="Left" ItemsSource="{Binding}"
Margin="871,126,0,0" Name="questionsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top" Width="400"
CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="id_QuestionColumn1" Header="id Question" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=id_Question}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="questionTextColumn1"
Binding="{Binding Path=QuestionText}" Header="Question Text" Width="300"
CanUserSort="True"/>
</DataGrid.Columns>
</DataGrid>
I have read the following among many other related articles / questions on Stack Overflow and other sites:
Each has provided useful insights but I haven't been able to get it to work for my problem. What do I need to write in the code behind file to get this to work properly?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的模板列是否未排序或任何列未排序?
如果模板列有问题,那么...
设置
Is your template column not sorting or ANY column is not sorting?
If its issue with template column thenn ...
Set
我遇到了同样的问题并花了很多时间寻找。最后,解决方案非常简单:
问题是 CollectionViewSource,它默认创建一个 BindingListCollectionView,它实现了不支持排序的 IBindingList!
但是 CollectionViewSource 具有 CollectionViewType 属性,它允许将视图类型更改为 ListCollectionView,从而允许排序。就这样。
我今天刚刚在代码中修复了它,所以我还不知道有什么副作用。目前,一切似乎都进展顺利。
I faced the same problem and spent a lot of time searching. In the end, the solution was VERY simple:
The problem is the CollectionViewSource, which creates by default a BindingListCollectionView, which implements IBindingList which does not support sorting !
But CollectionViewSource has the property CollectionViewType which allows changing the view type to ListCollectionView, which allows sorting. That's all.
I just fixed it today in my code, so I don't know yet what are the side effects. Presently, everything seems to work fine.