如何禁用ListBox中的ScrollViewer?
我有一个列表框。它有内部 ScrollViewer,因此我可以用鼠标滚轮滚动 ListBox 内容。它工作正常,直到我设置包含另一个 ListBox 的项目模板(事实上,我有 4 个嵌套的 ListBox =))。问题是内部 ListBox 的 ScrollViewer 窃取旋转事件。有什么简单的方法可以防止这种行为吗?
我有带有 ItemContainerStyle 的 ListBox,如下所示:
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="BorderBrush" Value="Black"/>
...
</Style>
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}" />
How can I set style for ItemContainer's item border in resources like this?据我所知 ContentPresenter 是 ItemsControl 的项目容器。但它没有边框,所以我无法设置它的样式。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过将控件模板更改为更简单的内容,从
ListBox
中删除ScrollViewer
:但是,我质疑嵌套 ListBox 的价值。请记住,每个
ListBox
都是一个选择器,并且具有“选择”哪个项目的概念。在所选项目中包含所选项目真的有意义吗?我建议将“内部”
ListBoxes
更改为简单的ItemsControls
,以便嵌套列表不能包含选定的项目。这将带来更简单的用户体验。您可能仍然需要以相同的方式重新模板化内部ItemsControls
以删除滚动条,但至少用户不会对“选择”哪个项目感到困惑。You can remove the
ScrollViewer
from aListBox
by changing its control template to something much simpler:However, I question the value of nesting ListBoxes. Remember that each
ListBox
is a Selector and has a concept of which item is "selected". Does it really make sense to have a selected item inside a selected item, inside a selected item?I would suggest changing the "inner"
ListBoxes
to simpleItemsControls
so that the nested lists can't have selected items. That would make for a much simpler user experience. You may still need to retemplate the innerItemsControls
in the same way to remove the scrollbars, but at least the user won't get confused about which item is "selected".您可以通过在 XAML 中捕获滚动事件来禁用窃取滚动事件:
并在后面的代码中重新发布它:
该解决方案完全适用于 ListBox,它对 ListView 很有帮助。
我在这里找到了这个解决方案:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/3a3bb6b0-e088-494d-8ef2-60814415fd89/swallowing-mouse-scroll?forum=wpf
You can disable stealing scroll events by catching scroll event in XAML:
and re-publishing it in Code behind:
The solution is exactly for ListBox, it helped me with ListView.
I found this solution here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/3a3bb6b0-e088-494d-8ef2-60814415fd89/swallowing-mouse-scroll?forum=wpf
我喜欢为这类事情创造一种行为。
xmlns:bhv="http://schemas.microsoft.com/xaml/behaviors"
行为本身。
I like to create a behavior for this type of thing.
xmlns:bhv="http://schemas.microsoft.com/xaml/behaviors"
The behavior itself.
抱歉让这么老的帖子醒来。实际上,您可以使用 ScrollViewer 的附加属性来禁用 ScrollViewer。
Sorry for waking up such a old post. Actually, you can disable the ScrollViewer by using ScrollViewer's attached property.
这是 DependencyProperty 的变体,如果您不喜欢行为,
这就是它的使用方式
Here is a variant with DependencyProperty, if you don't like behaviors
This is how it is used
你可以用这个!没有车轮被盗。
You can use this ! No Wheel stolen.