UWP C# Scroll to the bottom of TextBox

前端 未结 3 926
说谎
说谎 2020-12-18 13:38

How do you scroll to the bottom of a TextBox for a UWP app?

With my transition to UWP, this has been one of the questions that hasn\'t been straight-forward.

3条回答
  •  时光取名叫无心
    2020-12-18 14:03

    If anyone needs to scroll to the bottom of a TextBox in UWP apps:

    https://code.msdn.microsoft.com/windowsapps/How-to-scroll-to-the-a8ea5867

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
        var grid = (Grid)VisualTreeHelper.GetChild(textBox1, 0); 
        for (var i = 0; i <= VisualTreeHelper.GetChildrenCount(grid) - 1; i++) 
            { 
                object obj = VisualTreeHelper.GetChild(grid, i); 
                if (!(obj is ScrollViewer)) continue; 
                ((ScrollViewer)obj).ChangeView(0.0f, ((ScrollViewer)obj).ExtentHeight, 1.0f); 
                break; 
            } 
        }
    }
    

    where textBox1 is the TextBox you want to scroll to the bottom.

提交回复
热议问题