重置 WPF Datagrid 滚动条位置

发布于 2024-10-19 08:55:15 字数 401 浏览 7 评论 0原文

当更改 Datagrid 的 .DataContext 属性(更改为新源)时,所选项目将被清除,但滚动条位置将保留。为了避免这种情况,我在更改数据上下文后调用 .ScrollIntoView(.Item(0) 来向上移动滚动条。但它会在几分之一秒内显示错误的页面,当我在更改数据上下文之前滚动到顶部时,我有同样的问题,

那么如何同时更改 .DataContext 并重置滚动条位置?

编辑:我应该提到我的 XAML 看起来像这样:

<DataGrid VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"> 

所以也许虚拟化是原因。

When changing the .DataContext property of a Datagrid (to a new source) the selected item gets cleared, but the scrollbar position is retained. To avoid this I call .ScrollIntoView(.Item(0), after changing the datacontext, to move the scrollbar upwards. But it displays the wrong page for a fraction of a second, and when I scroll to the top before changing the datacontext, i have the same problem.

So how can I change the .DataContext and resetting the scrollbar position at the same time?

EDIT: I should mention that my XAML looks like this:

<DataGrid VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"> 

So maybe the virtualizing is the cause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2024-10-26 08:55:15

您是否尝试过在 DataContextChanged 事件中为 ScrollViewer 调用 ScrollToTop

<DataGrid VirtualizingStackPanel.IsVirtualizing="True"
          VirtualizingStackPanel.VirtualizationMode="Recycling"
          DataContextChanged="dataGrid_DataContextChanged"
          ...>

private void dataGrid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(dataGrid);
    if (scrollViewer != null)
    {
        scrollViewer.ScrollToTop();
    }
}

获取VisualChild

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;
}

Have you tried calling ScrollToTop for the ScrollViewer in the DataContextChanged event?

<DataGrid VirtualizingStackPanel.IsVirtualizing="True"
          VirtualizingStackPanel.VirtualizationMode="Recycling"
          DataContextChanged="dataGrid_DataContextChanged"
          ...>

private void dataGrid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(dataGrid);
    if (scrollViewer != null)
    {
        scrollViewer.ScrollToTop();
    }
}

GetVisualChild

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文