Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9280663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:55:37+00:00 2026-06-18T17:55:37+00:00

I would like to allow click and drag scrolling using a ScrollViewer (i.e. click

  • 0

I would like to allow click and drag scrolling using a ScrollViewer (i.e. click anywhere in the ScrollViewer and drag up or down, and it will scroll accordingly)

I have a StackPanel nested inside a ScrollViewer and I already have the scrolling working. I believe I saw that answer somewhere, but I can’t seem to find it anymore.

This has to be done using code only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:55:38+00:00Added an answer on June 18, 2026 at 5:55 pm

    Look at this code from Matt Hamilton:

    public class TouchScrolling : DependencyObject
    {
        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnabledProperty);
        }
    
        public static void SetIsEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsEnabledProperty, value);
        }
    
        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }
    
        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(TouchScrolling), new UIPropertyMetadata(false, IsEnabledChanged));
    
        static Dictionary<object, MouseCapture> _captures = new Dictionary<object, MouseCapture>();
    
        static void IsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as ScrollViewer;
            if (target == null) return;
    
            if ((bool)e.NewValue)
            {
                target.Loaded += target_Loaded;
            }
            else
            {
                target_Unloaded(target, new RoutedEventArgs());
            }
        }
    
        static void target_Unloaded(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Target Unloaded");
    
            var target = sender as ScrollViewer;
            if (target == null) return;
    
            _captures.Remove(sender);
    
            target.Loaded -= target_Loaded;
            target.Unloaded -= target_Unloaded;
            target.PreviewMouseLeftButtonDown -= target_PreviewMouseLeftButtonDown;
            target.PreviewMouseMove -= target_PreviewMouseMove;
    
            target.PreviewMouseLeftButtonUp -= target_PreviewMouseLeftButtonUp;
        }
    
        static void target_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var target = sender as ScrollViewer;
            if (target == null) return;
    
            _captures[sender] = new MouseCapture
            {
                VerticalOffset = target.VerticalOffset,
                Point = e.GetPosition(target),
            };
        }
    
        static void target_Loaded(object sender, RoutedEventArgs e)
        {
            var target = sender as ScrollViewer;
            if (target == null) return;
    
            System.Diagnostics.Debug.WriteLine("Target Loaded");
    
            target.Unloaded += target_Unloaded;
            target.PreviewMouseLeftButtonDown += target_PreviewMouseLeftButtonDown;
            target.PreviewMouseMove += target_PreviewMouseMove;
    
            target.PreviewMouseLeftButtonUp += target_PreviewMouseLeftButtonUp;
        }
    
        static void target_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var target = sender as ScrollViewer;
            if (target == null) return;
    
            target.ReleaseMouseCapture();
        }
    
        static void target_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (!_captures.ContainsKey(sender)) return;
    
            if (e.LeftButton != MouseButtonState.Pressed)
            {
                _captures.Remove(sender);
                return;
            }
    
            var target = sender as ScrollViewer;
            if (target == null) return;
    
            var capture = _captures[sender];
    
            var point = e.GetPosition(target);
    
            var dy = point.Y - capture.Point.Y;
            if (Math.Abs(dy) > 5)
            {
                target.CaptureMouse();
            }
    
            target.ScrollToVerticalOffset(capture.VerticalOffset - dy);
        }
    
        internal class MouseCapture
        {
            public Double VerticalOffset { get; set; }
            public Point Point { get; set; }
        }
    

    }

    There are some quirks here. I noticed that the ScrollViewer was actually being loaded, unloaded and loaded again when the content was shown.

    That meant that I couldn’t just hook up the events in the IsEnabledChanged method and unhook them in the target_Unloaded event handler, because they were being unhooked immediately. Instead, I’ve had to hook them up in a handler for the Loaded event, which in turn never gets unhooked.

    That means that there’s something of a “memory leak” in there, but it’s one I’m prepared to live with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to allow the user to click within my UserControl and drag
I have an HTML Table and would like to allow the user to click
I would like to allow a user to click on a JList and if
I have a ASP.NET 3.5 web application and I would like to allow users
I would like to be able to allow the user to click a button
I have a file listing in my application and I would like to allow
I have a borderless window (BorderStyle = None) where I would like to allow
I've a user profile and would like allow the user update everything about himself(username,
I've got a html-page with a picture on it and would like allow the
I would like to allow users to call my ruby on rails app as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.