开发者

Silverlight 4 Page Scroll on TAB hit

hey guys, 开发者_高级运维I have a silverlight Navigation application where on one of my pages I have a form to be filled out by the end user, which has a couple of TextBoxes to be filled, the problem is that when I hit the TAB key to move to the next TextBox the cursor does move to the next TextBox in sequence but the page does not scroll down to the new TextBox so i cant see the TextBox even though the cursor is in there....does anybody know the solution for this?? I appreciate any help.

Thank you, Brahim


Here's the test XAML:

<ScrollViewer x:Name="scrollViewer">
    <StackPanel x:Name="stackPanel"
                Orientation="Vertical">

        <TextBox Width="100"
                 Text="#1"
                 GotFocus="TextBox_GotFocus" />

        <Rectangle Fill="AliceBlue"
                   Width="100"
                   Height="400" />

        <TextBox Width="100"
                 Text="#2"
                 GotFocus="TextBox_GotFocus" />

        <Rectangle Fill="AliceBlue"
                   Width="100"
                   Height="400" />

        <TextBox Width="100"
                 Text="#3"
                 GotFocus="TextBox_GotFocus" />
    </StackPanel>
</ScrollViewer>

Code for the 'TextBox_GotFocus' event:

private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
    GeneralTransform gt = ((TextBox)sender).TransformToVisual(this);
    Point textBoxPositionRelativeToControl = gt.Transform(new Point(0, 0));

    if (textBoxPositionRelativeToControl.Y > this.ActualHeight
        || textBoxPositionRelativeToControl.Y < this.ActualHeight)
    {
        gt = ((TextBox)sender).TransformToVisual(stackPanel);
        Point textBoxPositionRelativeToStackPanel = gt.Transform(new Point(0, 0));

        scrollViewer.ScrollToVerticalOffset(textBoxPositionRelativeToStackPanel.Y - this.ActualHeight / 2);
    }
}

Updated the code so that if the textbox is outside the visible area then the scrollviewer centers the hidden textbox; otherwise, nothing happens. Works if you tab to the next or previous textbox (shift + tab).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜