簡體   English   中英

WPF-如何在需要滾動時自動滾動scrollviewer?

[英]WPF - How to autoscroll scrollviewer when it need to scroll?

讓我嘗試解釋一下我的要求是什么,首先,這是一個包含50個字段的表單,開始時,光標位於第一個字段TextBox中:

在此處輸入圖片說明

當我填寫10個字段時,光標現在將位於Field11中:

在此處輸入圖片說明

現在,當我將重點放在Field11上以獲取更多視圖時,我希望滾動條可以自動滾動到這樣的位置:

在此處輸入圖片說明

因此,如果有人了解我在說什么,請您幫我解決這個問題? 謝謝!

您可以使用ScrollChangedEventArgs.ExtentHeightChange來了解ScrollChanged是由於內容更改還是用戶操作引起的...當內容不變時,ScrollBar位置將設置或取消設置自動滾動模式。 內容更改后,您可以應用自動滾動。

后面的代碼:

private Boolean AutoScroll = true;

private void ScrollViewer_ScrollChanged(Object sender, ScrollChangedEventArgs e)
{
    // User scroll event : set or unset autoscroll mode
    if (e.ExtentHeightChange == 0)
    {   // Content unchanged : user scroll event
        if (ScrollViewer.VerticalOffset == ScrollViewer.ScrollableHeight)
        {   // Scroll bar is in bottom
            // Set autoscroll mode
            AutoScroll = true;
        }
        else
        {   // Scroll bar isn't in bottom
            // Unset autoscroll mode
            AutoScroll = false;
        }
    }

    // Content scroll event : autoscroll eventually
    if (AutoScroll && e.ExtentHeightChange != 0)
    {   // Content changed and autoscroll mode set
        // Autoscroll
        ScrollViewer.ScrollToVerticalOffset(ScrollViewer.ExtentHeight);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM