WPF列表视图修改
出于一个非常具体的原因,我想在鼠标按钮松开时选择 ListViewItem
s,而不是实际上在按下鼠标按钮时选择。我希望将此行为嵌入到控件中。有可能实现这一目标吗?谁能给出提示吗?
For a very specific reason I want to select ListViewItem
s on mouse button up, not actually on mouse button down. I want this behaviour to be embedded in the control. Is it possible to achieve this? can anyone give hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,使用附加属性绝对是可能的。定义一个名为
SelectOnMouseUp
的附加属性,当它设置为 true 时,挂钩到ItemsContainerGenerator
事件以发现何时添加新的项目容器。然后,当您收到新项目容器的事件时,挂钩其PreviewMouseDown
并忽略它(将e.Handled
设置为 true),然后挂钩其MouseUp
事件并执行选择(将IsSelected
设置为true
)。Yes it's definitely possible using attached properties. Define an attached property called
SelectOnMouseUp
and when it's set to true, hook to yourItemsContainerGenerator
events to discover when a new item container is added. Then when you get an event for a new item container, hook into itsPreviewMouseDown
and ignore it (sete.Handled
to true), and hook into itsMouseUp
event and perform the selection (setIsSelected
totrue
).Aviad P. 的答案是一个很好的答案,并且巧妙地使用了附加属性,但我大多数时候倾向于使用不同的技术:
这对我来说似乎比订阅 ItemContainer 事件并动态添加处理程序更容易。
它是这样的:
我喜欢这种技术,因为涉及的代码较少。
Aviad P.'s answer is a good one and a clever use of attached properties, but I tend to use a different technique most of the time:
This seems easier to me than subscribing to ItemContainer events and adding handlers dynamically.
This is what it looks like:
I like this technique because there is less code involved.