WPF 数据网格 - 当可编辑单元格“打开”时,具有可编辑列的数据网格分组会对组中的行进行重新排序。或编辑过的
分组后,我在 WPF 数据网格中遇到一个奇怪的问题。组内的行开始重新排序。我正在使用 codeplex 的 .net 3.5 框架的数据网格。
该问题已在msdn论坛中提出。请找到下面的链接。
http://social.msdn.microsoft.com/Forums/en/wpf/thread/3f915bf9-2571-43e8-8560-3def1d0d65bb
msdn用户在线程末尾说道可能没有解决方法。但我非常需要一个!
I am facing a strange problem in WPF datagrid after grouping. The rows inside groups start reordering. I am using the .net 3.5 framework's datagrid from codeplex.
The problem has been asked in msdn forum. Please find the link below.
http://social.msdn.microsoft.com/Forums/en/wpf/thread/3f915bf9-2571-43e8-8560-3def1d0d65bb
The msdn user has says at the end of the thread that there might be no workarounds. But I badly need one !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过通过 CustomSort 进行排序?我在某个地方看到了这篇文章,它对我有用。虽然需要一些查找,但可以通过
ListCollectionView
类获得。类似于:其中
YourObjectComparer
实现IComparer
并根据您想要的属性进行排序。Have you tried doing your sorting via a CustomSort? I saw this posted somewhere and it worked for me. It took a little bit of finding, but it's available via the
ListCollectionView
class. So something like:Where
YourObjectComparer
implementsIComparer
and sorts on the properties that you want to.编辑:
为此,当进行单元格编辑时,我们会删除组并再次添加它们。这使得细胞保持其预期的顺序。但这又带来了另一个问题,即所有扩展器都塌陷了。因此,如果我们记住哪些扩展器在重新组合后仍然保持扩展状态,我们就可以实现您所寻求的目标。
有一些事件可以帮助您实现此功能...
DataGrid.CellEditEnding
事件Expander.Initialized
、Expander.Expanded
和 < code>Expander.Collpased 事件ICollectionView.CurrentChanging
事件您需要记住的是扩展器展开或折叠时的状态。每个扩展器代表由
Name
属性表示的分组值。这些分组值是唯一的。因此,一个字典,其中Dictionary.Key
是这个Name
值,而Dictionary.Value
是Expander.IsExpanded
标志就足够了。有了这个原材料,下面的代码就可以满足您的需求...
模型类: 我表示 DataGrid 中键值对象的简单列表。 >
XAML:
Window.cs 代码隐藏:
但有两个权衡。
Name
键可能/可能不会保持唯一。例如,假设对于员工列表,如果您按FirstName
进行分组,并且还按LastName
进行分组,则会有嵌套的扩展器。现在,某些名字分组可能与某些姓氏分组相匹配,例如George
。这样字典就会陷入困境,无法正常工作。希望这有帮助。
EDIT:
For this when a cell edit takes place, we remove groups and add them again. This cuases the cell to stay on its intended order. But this poses another issue that is all the expanders are collapsed. So if we remember which expander(s) remains expanded after regrouping, we achieve what you seek.
There are a few events which can help you with achieving this functionality...
DataGrid.CellEditEnding
eventExpander.Initialized
,Expander.Expanded
andExpander.Collpased
eventsICollectionView.CurrentChanging
eventAll you need to remember is the status of the expanders when they are expanded or collapsed. Each expander represents the grouped value represented by
Name
property. These grouped values are unique. So a dictionary whereDictionary.Key
is thisName
value andDictionary.Value
is theExpander.IsExpanded
flag would just suffice.With this raw material following code does what you seek...
Model Class: I represent a simple list of Key-Value objects in the DataGrid.
XAML:
Window.cs Code Behind:
But there are two trade offs.
Name
key in the dictionary may / may not stay unique. E.g. Assume for employee list if you group onFirstName
and also gropup byLastName
there will be nested expanders. Now some first name grouping may match with some last name grouping such asGeorge
. So the dictionary will fall into a trick and will not work correctly.Hope this helps.