简体   繁体   English

UWP 获取Datagrid的ScrollViewer的垂直偏移量

[英]UWP get the vertical offset of Datagrid's ScrollViewer

I'm using a datagrid from "Microsoft.Toolkit.Uwp.UI.Controls" and I'm trying to get the scrollViewer that is built into it to be able to get the vertical offset and implement certain behavior when scrolled all the way to the bottom or the middle.我正在使用“Microsoft.Toolkit.Uwp.UI.Controls”中的数据网格,我试图让内置的 scrollViewer 能够获得垂直偏移并在一直滚动到时实现某些行为底部或中间。 So far I've tried a number of methods I found in other different threads like the one below, that scans the visual tree and gets the scrollViewer.到目前为止,我已经尝试了许多在其他不同线程中找到的方法,例如下面的方法,它们扫描可视化树并获取 scrollViewer。 But the value of the verticalOffset property of the scrollViewer returned from this method is always zero and it's events like ViewChanged never gets fired, I've tried calling the updateLayout method but it changed nothing.但是从此方法返回的 scrollViewer 的 verticalOffset 属性的值始终为零,并且像 ViewChanged 这样的事件永远不会被触发,我尝试调用 updateLayout 方法,但它什么也没改变。

I've also tried wrapping the datagrid in a scrollviewer and used that instead.我还尝试将数据网格包装在滚动查看器中并改用它。 While that worked fine, it caused a huge performance issue due to virtualization.虽然效果很好,但由于虚拟化,它导致了巨大的性能问题。 So is there any solution to this?那么有什么解决方案吗?

private ScrollViewer GetScrollViewer(UIElement element)
        {
            if (element == null) return null;

            ScrollViewer retour = null;
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element) && retour == null; i++)
            {
                if (VisualTreeHelper.GetChild(element, i) is ScrollViewer)
                {
                    retour = (ScrollViewer)(VisualTreeHelper.GetChild(element, i));
                }
                else
                {
                    retour = GetScrollViewer(VisualTreeHelper.GetChild(element, i) as UIElement);
                }
            }
            return retour;
        }

As Oleg Mikhailov mentioned the source code , it was not implemented with ScrollViewer , and we have tested with your code, we can't get the scrollviewer instance.正如 Oleg Mikhailov 提到的源代码,它不是用ScrollViewer实现的,我们已经用你的代码进行了测试,我们无法获取 scrollviewer 实例。 for this scenario, you could detect vertical scrollbar's value change event to get vertical offset, please refer the following code.对于这种情况,您可以检测垂直滚动条的值更改事件来获取垂直偏移量,请参考以下代码。

var scrollbar = MyFindDataGridChildOfType<ScrollBar>(MyDataGrid);
scrollbar.ValueChanged += Scroll_ValueChanged;
private void Scroll_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    Debug.WriteLine(e.NewValue);
   // here is vertical value.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM