问题描述
我有一个用户控件,它有一个滚动查看器,然后是一堆子控件,如文本框、单选按钮和列表框等.我可以使用鼠标滚轮滚动父滚动查看器,直到我的鼠标落在列表框内,然后鼠标滚轮事件开始进入列表框.
I have a usercontrol that has a scrollviewer, then a bunch of child controls like text boxes, radio buttons, and listboxes, etc inside of it. I can use the mouse wheel to scroll the parent scrollviewer until my mouse lands inside a listbox then, the mouse wheel events start going to the listbox.
有没有办法让列表框将这些事件发送回父控件?像这个问题建议的那样从父控件内部删除列表框(在 ScrollViewer 的子控件上方时鼠标滚轮不起作用) 不是解决方案.
Is there any way to have the listbox send those events back up to the parent control? Removing the listbox from within side the parent control like this question suggests (Mouse wheel not working when over ScrollViewer's child controls) isnt a solution.
我试过了
private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
但这也没用.
谢谢
推荐答案
您引用的答案正是导致您的问题的原因,您的 ScrollViewer 中的 ListBox(由 ScrollViewer 等组成)捕获了 MouseWheel 事件并处理它,防止它冒泡,因此 ScrollViewer 不知道该事件曾经发生过.
The answer you have referenced is exactly what is causing your problem, the ListBox (which is composed of among other things a ScrollViewer) inside your ScrollViewer catches the MouseWheel event and handles it, preventing it from bubbling and thus the ScrollViewer has no idea the event ever occurred.
为您的 ListBox 使用以下极其简单的 ControlTemplate 进行演示(注意它没有 ScrollViewer,因此不会捕获 MouseWheel 事件) ScrollViewer 仍然会随着鼠标在 ListBox 上滚动.
Use the following extremely simple ControlTemplate for your ListBox to demonstrate (note it does not have a ScrollViewer in it and so the MouseWheel event will not be caught) The ScrollViewer will still scroll with the mouse over the ListBox.
<UserControl.Resources>
<ControlTemplate x:Key="NoScroll">
<ItemsPresenter></ItemsPresenter>
</ControlTemplate>
</UserControl.Resources>
<ScrollViewer>
<SomeContainerControl>
<.... what ever other controls are inside your ScrollViewer>
<ListBox Template="{StaticResource NoScroll}"></ListBox>
<SomeContainerControl>
</ScrollViewer>
您确实可以选择在鼠标进入 ScrollViewer 时捕获鼠标,因此它会继续接收所有鼠标事件,直到鼠标被释放,但是此选项将要求您将任何进一步的鼠标事件委托给包含在 ScrollViewer 中的控件ScrollViewer 如果您需要响应...以下 MouseEnter MouseLeave 事件处理程序就足够了.
You do have the option of capturing the mouse when it enters the ScrollViewer though so it continues to receive all mouse events until the mouse is released, however this option would require you to delgate any further mouse events to the controls contained within the ScrollViewer if you want a response...the following MouseEnter MouseLeave event handlers will be sufficient.
private void ScrollViewerMouseEnter(object sender, MouseEventArgs e)
{
((ScrollViewer)sender).CaptureMouse();
}
private void ScrollViewerMouseLeave(object sender, MouseEventArgs e)
{
((ScrollViewer)sender).ReleaseMouseCapture();
}
然而,我提供的解决方法都不是真正的首选,我建议重新考虑您实际尝试做的事情.如果您在问题中解释您要达到的目标,我相信您会得到更多建议...
Neither of the workarounds I have provided are really preferred however and I would suggest rethinking what you are actually trying to do. If you explain what you are trying to achieve in your question I'm sure you will get some more suggestions...
这篇关于在 wpf 中禁用项目控件上的鼠标滚轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!