Wpf Datagrid在顶部滚动,默认情况下取消全部分组

时间:2015-01-23 17:55:05

标签: c# wpf datagrid wpfdatagrid

我正在开发一个wpf图片库。我想要做的是当用户一直滚动到顶部时。更多图像是从数据库中提取的。我无法找到datagrid的滚动事件?。其次我使用了datagrid的分组功能,但它始终在顶部滚动,默认选择第一项。有一种方法可以默认取消全部并将滚动条放在底部?。提前谢谢

1 个答案:

答案 0 :(得分:0)

首先应该获取scrollViewer,然后需要ScrollChanged事件来描述

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(dgFamily);
    if (scrollViewer != null)
        scrollViewer.ScrollChanged += scrollViewer_ScrollChanged;
}
void scrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    if (e.VerticalOffset == 0)
        MessageBox.Show("You reach to top");
}
private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);

    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}