2017-05-17 5 views
-1

まず、タイトルが意味をなさないかもしれません。それを変更するための提案は高く評価されます。WPF Scrollviewerが高さを収縮させ、要素を表示させる

ScrollViewerの中にあるTextBoxをクリックしています。その場合、ScrollViewerは高さが下から上に収縮し、まったくスクロールせず、ビューポートの下部にあるいくつかのコントロールが覆い隠されます(ビューポートが小さくなるため)。 TextBoxが隠れてしまった場合は、スクロールしてまだ見えるようにする必要があります。

私はいくつかのSOの質問をチェックしましたが、何も私の問題を捉えていないようです。 This oneは近いですが、私は作業するキャンバスを持っていません。また、具体的なシナリオを考えてみると、できません。Dispatcherを使用してUIが読み込まれるまで待ってからBringIntoView()を使用してください。

TextBoxのシェアイベント、TextBox_GotFocus

TextBox_GotFocus(object sender, RoutedEventArgs e) 
{ 
    myScrollViewer.Height = 400; //used to be 600 

    //if sender was in the 401-600 range, bring it into view 
} 

私はScrollViewerをスクロールするにはどうすればよい入っTextBoxは、現在の高さを変更した後に隠されている場合のみ?

答えて

0

ランダムなdownvoteのせいではありませんが、私はこれを行うためにラウンドアバウトの方法を理解することができました。

TextBox_GotFocus(object sender, RoutedEventArgs e) 
{ 
    FrameworkElement element = sender as FrameworkElement; 

    //Get the distance from the top and bottom of the ScrollViewer 
    double offsetTop = element.TranslatePoint(new Point(), myScrollViewer).Y; 
    double offsetBottom = myScrollViewer.Height - offsetTop; 

    //Get total height needed to show the whole element 
    double height = 200 + element.Height; 

    //If the control would be hidden... 
    if (offsetBottom < height) 
    { 
     //Scroll down the difference 
     double change = myScrollViewer.VerticalOffset + (height - offsetBottom); 
     myScrollViewer.ScrollToVerticalOffset(change); 
    } 

    myScrollViewer.Height = 400; //used to be 600 
} 
関連する問題