简体   繁体   中英

Unable to scroll (broken TextBox) after Select and ChangeView in UWP

I just want to scroll to my regex result. I notice that if I only use Select or only ChangeView , there is no error (but no result too).

private void showrez()
        {
            if (head != null)
            {
                ContentBox.Focus(FocusState.Programmatic);
                ContentBox.Select(head.Index, head.Length);
                var r = ContentBox.GetRectFromCharacterIndex(ContentBox.SelectionStart, false);

                var grid = VisualTreeHelper.GetChild(ContentBox, 0);
                for (var i = 0; i < VisualTreeHelper.GetChildrenCount(grid); i++)
                {
                    object obj = VisualTreeHelper.GetChild(grid, i);
                    if (obj is ScrollViewer)
                    {
                        ((ScrollViewer)obj).ChangeView(r.Left, r.Top, null);
                        break;
                    }
                }
            }
        }

EDIT:

I just checked this works without bugs , but used methods are obsolete.

private void showrez()
        {
            if (head != null)
            {
                ContentBox.Focus(FocusState.Programmatic);
                ContentBox.Select(head.Index, head.Length);
                var r = ContentBox.GetRectFromCharacterIndex(ContentBox.SelectionStart, false);

                var grid = VisualTreeHelper.GetChild(ContentBox, 0);
                for (var i = 0; i < VisualTreeHelper.GetChildrenCount(grid); i++)
                {
                    object obj = VisualTreeHelper.GetChild(grid, i);
                    if (obj is ScrollViewer)
                    {
                        //((ScrollViewer)obj).ChangeView(r.Left, r.Top, null);

                        ((ScrollViewer)obj).ScrollToVerticalOffset(r.Top);
                        ((ScrollViewer)obj).ScrollToHorizontalOffset(r.Left);

                        break;
                    }
                }

            }
        }

When we use the ChangeView(IReference<Double>, IReference<Double>, IReference<Single>) method it enable scrolling animation by default. Please try to use the ChangeView(IReference<Double>, IReference<Double>, IReference<Single>, Boolean) and we should be able to set true to disable zoom/pan animations while changing the view. The default is false.

Foe example:

((ScrollViewer)obj).ChangeView(r.Left, r.Top, null,true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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