UWP C# Scroll to the bottom of TextBox

前端 未结 3 935
说谎
说谎 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:21

    An equivalent to the previous answer in C++/CX:

    using Windows::UI::Xaml::Media::VisualTreeHelper;
    using Windows::UI::Xaml::Controls::Grid;
    using Windows::UI::Xaml::Controls::ScrollViewer;
    using Platform::Object;
    
    void
    MainPage::responseTextUpdated(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        Grid^ grid = static_cast(VisualTreeHelper::GetChild(responseText, 0));
        for (int i = 0; i < VisualTreeHelper::GetChildrenCount(grid); ++i)
        {
            Object^ child = VisualTreeHelper::GetChild(grid, i);
            ScrollViewer^ scrollViewer = dynamic_cast(child);
            if (scrollViewer == nullptr) continue;
    
            double const horizontalOffset = 0;
            double const verticalOffset = scrollViewer->ExtentHeight;
            float const zoomFactor = 1;
    
            scrollViewer->ChangeView(horizontalOffset, verticalOffset, zoomFactor);
            break;
        }
    }
    

    Where responseText is TextBox^ responseText, the TextBox you want to scroll (probably the same as sender).

提交回复
热议问题