How to disable scrolling in ScrollViewer in AvaloniaUI?

261 views Asked by At

I am using MapControl(MapsUI) in ScrollViewer in my AvaloniaUI 11.0.5. app. I need to disable scrolling in ScrollViewer while pointer is in MapControll. I have tried this code but it did not work:

AXAML:

<ScrollViewer Name="mainPanel">
...
<Panel PointerEntered="disableScroll" PointerExited="enableScroll" Name="MapBox" /> //here is MapControl added later in code
...
///some very big stuff here like Panels, TextBoxes, Images etc.
...
</ScrollViewer>

c#:

private void disableScroll(object o, PointerEventArgs e)
{
    mainPanel.VerticalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Disabled;
}

private void enableScroll(object o, PointerEventArgs e)
{
    mainPanel.VerticalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Auto;
}

Maybe is there some pre-scroll event? Any ideas please?

2

There are 2 answers

0
AdamA On BEST ANSWER

After very long time I found a solution.

I created function:

private void MapControl_PointerWheelChanged(object sender, PointerWheelEventArgs e)
{
    e.Handled = true;
}

and and assigned it to MapControl in axaml:

<avalonia:MapControl Name="Map" PointerWheelChanged="MapControl_PointerWheelChanged"/>

Now I can zoom in and out the Map without scrolling the ScrollViewer at the same time.

5
Tarazed On

The ability to scroll in a ScrollViewer can be controlled by the visibility of the ScrollBar itself.

<ScrollViewer Name="MyScrollViewer">
private void DisableScroll()
{
    MyScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
}

private void EnableScroll()
{
    MyScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible; // Or Auto if prefered.
}

You can also bind this in XAML if you prefer an MVVM approach.

WARNING: Hiding the Scrollbar does not disable scrolling using the mouse wheel. Only ScrollBarVisibility.Disabled will stop all scrolling. Though you might be able to achieve a Disabled Hidden effect using a style and a Property Equals selector.