Search code examples
c#wpfscrollview

WPF C# Make ScrollBar Clickable Only


I am creating an application which currently lets users scroll to zoom into an image. Currently, if I try to use a scrollview to allow users to scroll down the page, it breaks this feature. Is there any way for me to implement a scrollbar that does not access the scroll wheel? What I am looking for is a way to only allow users to access the scrollbar by clicking it, not by using the scrollwheel.


Solution

  • Set the MouseWheel event on your image and set e.Handled = true at the end.

    Contrived example -

    XAML:

    <Grid>
        <ScrollViewer>
            <Canvas Width="1000" Height="1000" Background="PeachPuff" MouseWheel="Canvas_MouseWheel" />
        </ScrollViewer>
    </Grid>
    

    Code:

        private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            (sender as Canvas).Background = Brushes.Blue;
            e.Handled = true;
        }