当 CanUserSortColumns=“False”时,如何让 DataGrid 列显示鼠标悬停和排序方向?
我想为 WPF DataGrid
提供我自己的排序行为实现,因为我使用的是分页数据模型。我的分页行为运行良好,但是 CanUserSortColumns="True"
的默认排序行为会导致当前页面进行排序,但我想捕获它并自己对其进行排序。
我想我可以通过侦听列标题上的点击事件并进行适当的排序来提供排序行为,这是可行的,但是当 CanUserSortColumns="False"
时,列标题不会响应鼠标交互不再显示排序方向指示器。
我认为情况就是如此,但我想知道如何恢复样式,而不必编写看起来像原始模板的整个模板。
I would like to provide my own implementation of sorting behaviour for the WPF DataGrid
as I am using a paged data model. My paging behaviour is working nicely, however the default sort behaviour for CanUserSortColumns="True"
causes the current page to sort, however I want to capture this and sort it myself.
I figured I'd be able to provide sorting behaviour by listening to click events on the column headers and sorting appropriately, which works, however when CanUserSortColumns="False"
the column headers do not respond to mouse interaction any more and do not show the sort direction indicator.
I figured this would be the case, but I wish to know how I can restore the style without having to write a whole template that looks like the original.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,我不记得是否可以获得对按钮或任何控件产生效果的特定样式。它们是这些控件的默认样式/模板,可能不可用。我记得模板只有在显示后才可用,除非明确设置。
但是,如果您的整个目标是实现自定义排序,您难道不能只处理
排序
事件?您甚至可以获得对Column 的引用
发起排序并得到SortDirection
值在排序之前(null
->升序
->降序
->)。然后您可以根据需要对源进行排序。Sorry, I can't remember whether or not it is possible to get the particular style which does the effects for the buttons or any control. They are default styles/templates for these controls and are probably not available. I do remember that templates are available only after shown unless explicitly set.
But, if your whole goal is to implement custom sorting, couldn't you just handle the
Sorting
event instead? You even get a reference to theColumn
that initiated the sorting and get theSortDirection
value prior to the sorting (null
->Ascending
->Descending
->). You could then sort your source as needed.您能否将 CanUserSortColumns 设置为 true,然后处理 PreviewLeftMouseButton,执行自定义操作,并将事件设置为处理,以便它永远不会进入默认排序逻辑?
Can you leave CanUserSortColumns set to true, then handle the PreviewLeftMouseButton down, do your custom stuff, and set the event to handled so that it doesn't ever get into the default sort logic?