How to autoscroll on WPF datagrid

后端 未结 17 955
甜味超标
甜味超标 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

    For having an AutoScroll To the Last element added :

    YourDataGrid.ScrollIntoView(YourDataGrid.Items.GetItemAt(YourDataGrid.Items.Count-1));
    

    May This Help :)

    0 讨论(0)
  • 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

    <Button x:Name="XBTNPageDown" Height="50" MouseLeftButtonDown="XBTNPageDown_MouseLeftButtonDown"  MouseUp="XBTNPageDown_MouseUp">Page Down</Button>
    

    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<ScrollViewer>();
                    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<T>(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<T>(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?

    0 讨论(0)
  • 2020-12-08 10:30

    ;)

    if (mainDataGrid.Items.Count > 0)
    {
        var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as Decorator;
        if (border != null)
        {
            var scroll = border.Child as ScrollViewer;
            if (scroll != null) scroll.ScrollToEnd();
        }
    }
    
    0 讨论(0)
  • 2020-12-08 10:30

    if large data datagrid.ScrollIntoView(itemInRow, column); not works fine then we need to use below one only:

    if (mainDataGrid.Items.Count > 0) 
            { 
                var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as Decorator; 
                if (border != null) 
                { 
                    var scroll = border.Child as ScrollViewer; 
                    if (scroll != null) scroll.ScrollToEnd(); 
                } 
            } 
    
    0 讨论(0)
  • 2020-12-08 10:30

    When you are use datagridview with the scrollbar must use this technique. becuase i was try other technique. but those are not work properly....

    var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as Decorator;
    if(border != null)
    {   var scroll = border.Child as ScrollViewer;
        if (scroll != null) scroll.ScrollToEnd(); 
    }
    
    0 讨论(0)
提交回复
热议问题