How to autoscroll on WPF datagrid

后端 未结 17 1014
甜味超标
甜味超标 2020-12-08 09:27

I think I am stupid. I searched now for 15 minutes, and found several different solutions for scrolling on datagrids, but none seems to work for me.

I am using WPF w

17条回答
  •  不知归路
    2020-12-08 10:28

    WPF DataGrid Auto Scrolling

    Auto Scrolling for as long as the the mouse button is down on a button control.

    The XAML

    
    

    The Code

        private bool pagedown = false;
        private DispatcherTimer pageDownTimer = new DispatcherTimer();
    
        private void XBTNPageDown_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pagedown = true;
            pageDownTimer.Interval = new TimeSpan(0, 0, 0, 0, 30);
            pageDownTimer.Start();
            pageDownTimer.Tick += (o, ea) =>
            {
                if (pagedown)
                {
                    var sv = XDG.FindVisualChild();
                    sv.PageDown();
                    pageDownTimer.Start();
                }
                else
                {
                    pageDownTimer.Stop();
                }
            };
        }
    
        private void XBTNPageDown_MouseUp(object sender, MouseButtonEventArgs e)
        {
            pagedown = false;
        }
    

    This is the extension method

    Place it in a static class of your choice and add reference to code above.

       public static T FindVisualChild(this DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
    
                    T childItem = FindVisualChild(child);
                    if (childItem != null) return childItem;
                }
            }
            return null;
        }
    

    NOTE: The property sv could be moved to avoid repeated work.

    Anyone have an RX way to do this?

提交回复
热议问题