Page 1 of 1

MouseWheel doesn't work if there are nested TreeViews

Posted: 30 May 2021, 11:12
by Logrus
Hi

I have a TreeView which in a TreeViewItem contains a TreeView, which is why the MouseWheel is not working.
To the TreeView I added a Behavior that handles the PreviewMouseWheel()
void MouseWheelBehavior::OnPreviewMouseWheel(BaseComponent* sender, const MouseWheelEventArgs& e)
{
	auto is_skip_scrollviewer = [](Noesis::ScrollViewer* viewer, int wheel) -> bool {
		if ((wheel > 0 && viewer->GetVerticalOffset() == 0) ||
			(wheel < 0 && viewer->GetVerticalOffset() >= viewer->GetExtentHeight() - viewer->GetViewportHeight())) {
			return true;
		}
		return false;
	};

	auto scroll = Gui::Helper::GetParentRecursive<Noesis::ScrollViewer>((Noesis::Visual*)e.source);
	while (scroll) {
		if (is_skip_scrollviewer(scroll, e.wheelRotation)) {
			scroll = Gui::Helper::GetParentRecursive<Noesis::ScrollViewer>((Noesis::Visual*)scroll);
		}
		else {
			e.handled = true;

			MouseWheelEventArgs eventArg{ scroll, UIElement::MouseWheelEvent, e.wheelRotation };	
			scroll->RaiseEvent(eventArg);

			break;
		}
	}
}
The handler goes up all the ScrollViewers and determines which ScrollViewer to redirect the MouseWheel message to.
It doesn't work quite right. Scrolling works even if any Popup elements are active.
I tried to call the MouseWheel message on the Parent of the ScrollViewer, but then the scrolling stops working
MouseWheelEventArgs eventArg{ scroll, UIElement::MouseWheelEvent, e.wheelRotation };	
scroll->GetUIParent()->RaiseEvent(eventArg);

Re: MouseWheel doesn't work if there are nested TreeViews

Posted: 31 May 2021, 22:04
by sfernandez
Does the inner TreeView need a ScrollViewer or is it fully fit inside the item?
Because you can use a different template for those inner TreeViews without the ScrollViewer:
<ControlTemplate x:Key="InnerTreeView" TargetType="TreeView">
    <ItemsPresenter />
</ControlTemplate>
This way only the outer TreeView will have a ScrollViewer and mouse wheel will work just fine.

Re: MouseWheel doesn't work if there are nested TreeViews

Posted: 31 May 2021, 23:20
by Logrus
Yes, I don't need internal ScrolViewers right now and this solution works.
But further scrolling may be required.

Re: MouseWheel doesn't work if there are nested TreeViews

Posted: 04 Jun 2021, 09:52
by sfernandez
Yes, I don't need internal ScrolViewers right now and this solution works.
If you have nested ScrollViewers and mouse is over inner one, it will handle the mouse events before they reach the outer scroll viewer. So this is the expected behavior and you should design your UI with that in mind.
But further scrolling may be required.
You can scroll a ScrollViewer control manually by using the public functions: LineUp, LineDown, PageUp, PageDown, ScrollToTop, ScrollToBottom, ScrollToVerticalOffset...
I don't know if that is what you mean.