Logrus
Topic Author
Posts: 52
Joined: 22 Jul 2015, 12:16

MouseWheel doesn't work if there are nested TreeViews

30 May 2021, 11:12

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);
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

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

31 May 2021, 22:04

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.
 
Logrus
Topic Author
Posts: 52
Joined: 22 Jul 2015, 12:16

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

31 May 2021, 23:20

Yes, I don't need internal ScrolViewers right now and this solution works.
But further scrolling may be required.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

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

04 Jun 2021, 09:52

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.

Who is online

Users browsing this forum: No registered users and 8 guests