WPF Datagrid滚动

时间:2017-02-22 10:48:23

标签: c# wpf datagrid

我正在使用WPF Datagrid,我希望在添加新行时自动滚动它。为此,我为ItemsSource添加了CollectionChanged事件,它工作正常。

'''
          2  + 0.2 = 2.2
         2.2 + 0.2 = 2.4
         2.4 + 0.2 = 2.6
         2.6 + 0.2 = 2.8
giving result 2.2 2.4 2.6 2.8
'''

问题是我想在单击Datagrid的任何滚动条时禁用自动滚动功能。我有什么方法可以做到这一点吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

经过一番调查,我找到了以下简单的解决方案。对于Datagrid,我添加了滚动事件。

XAML:

<DataGrid Name="dataGrid" ScrollBar.Scroll ="dataGrid_Scroll">
</DataGrid>

CS:

private void dataGrid_Scroll(object sender, RoutedEventArgs e)
    {
        ScrollEventArgs scrollEvent = e as ScrollEventArgs;
        if (scrollEvent != null)
        {
            if (scrollEvent.ScrollEventType == ScrollEventType.EndScroll)
            {
                isScrolling = false;
            }
            else
            {
                isScrolling = true;
            }
        }
    }

答案 1 :(得分:0)

您可以使用附加属性来观察相关的ScrollBar事件。请注意,在以下示例中,我使用两个属性:__all__以启用事件侦听器,EnableUserScrollingObserver报告当前滚动状态。

IsUserScrolling

然后将滚动信息连接到您的viewmodel public static class Attached { public static bool GetEnableUserScrollingObserver(DependencyObject obj) { return (bool)obj.GetValue(EnableUserScrollingObserverProperty); } public static void SetEnableUserScrollingObserver(DependencyObject obj, bool value) { obj.SetValue(EnableUserScrollingObserverProperty, value); } public static readonly DependencyProperty EnableUserScrollingObserverProperty = DependencyProperty.RegisterAttached("EnableUserScrollingObserver", typeof(bool), typeof(Attached), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(EnableUserScrollingObserverChanged))); private static void EnableUserScrollingObserverChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var s = d as ScrollBar; if (s != null) { s.Scroll -= Scrollbar_Scroll; if ((bool)e.NewValue) { s.Scroll += Scrollbar_Scroll; } } else { // Using this on anything other than a ScrollBar sucks throw new InvalidOperationException("EnableUserScrollingObserver is designed for ScrollBar elements!"); } } static void Scrollbar_Scroll(object sender, ScrollEventArgs e) { var s = sender as ScrollBar; switch (e.ScrollEventType) { case ScrollEventType.EndScroll: SetIsUserScrolling(s, false); break; /* All the things handled by default case ScrollEventType.First: break; case ScrollEventType.LargeDecrement: break; case ScrollEventType.LargeIncrement: break; case ScrollEventType.Last: break; case ScrollEventType.SmallDecrement: break; case ScrollEventType.SmallIncrement: break; case ScrollEventType.ThumbPosition: break; case ScrollEventType.ThumbTrack: break; */ default: SetIsUserScrolling(s, true); break; } } public static bool GetIsUserScrolling(DependencyObject obj) { return (bool)obj.GetValue(IsUserScrollingProperty); } public static void SetIsUserScrolling(DependencyObject obj, bool value) { obj.SetValue(IsUserScrollingProperty, value); } public static readonly DependencyProperty IsUserScrollingProperty = DependencyProperty.RegisterAttached("IsUserScrolling", typeof(bool), typeof(Attached), new FrameworkPropertyMetadata(false)); } 属性

IsScrolling

最后,在<Style TargetType="ScrollBar"> <Setter Property="local:Attached.EnableUserScrollingObserver" Value="True"/> <Setter Property="local:Attached.IsUserScrolling" Value="{Binding IsScrolling,Mode=OneWayToSource}"/> </Style> 方法中,添加对MainWindow_CollectionChanged属性的检查。