1
votes

I have some pages in my windows phone 7 application with many textboxes in it. As we know there is default behavior of windows phone 7 that when we got focus on any textbox SIP keyboard comes up and slides up the whole page and on lost focus it restores to normal state. I want to restrict this thing in my application. As I have googled and found solution is we place all our controls in scrollviewer and on got focus of textbox set scroll viewer veritcal offset to position of that textbox.

  1. Now I am confused how I can tell that scrollviewer about the position of my control for vertical offset.

  2. And Suppose If I can set that thing, I Need to call two methods for my textboxes on GotFocus and On LostFocus. Is there any other approach we can call this thing via interaction behavior and triggers.

UPDATE...

<Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="480" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <ScrollViewer x:Name="sc">
                <StackPanel>
                    <Grid x:Name="Grid1" Grid.Row="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
            </Grid.RowDefinitions>
                    <TextBox Width="460" AcceptsReturn="True" Grid.Row="0" x:Name="txtbox1"/>
                    <TextBox Width="460" AcceptsReturn="True" Grid.Row="1" x:Name="txtbox2"/>
                    <TextBox Width="460" AcceptsReturn="True" Grid.Row="2" x:Name="txtbox3"/>
                    <TextBox Width="460" AcceptsReturn="True" Grid.Row="3" x:Name="txtbox4"/>
                    <TextBox Width="460" AcceptsReturn="True" Grid.Row="4" x:Name="txtbox5"/>
                    <TextBox Width="460" AcceptsReturn="True" Grid.Row="5" x:Name="txtbox6" GotFocus="txtbox6_GotFocus"/>
            </Grid>
                </StackPanel>
            </ScrollViewer>
            <Grid x:Name="Grid2" Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="80" />
                </Grid.RowDefinitions>
                <Button x:Name="btnTest" Width="250" Content="Test" Grid.Row="0" />
            </Grid>
        </Grid>
    </Grid>

AND CS C# ....

void ScrollToFocused(ScrollViewer scrollviewer)
{
    FrameworkElement focusedElement = FocusManager.GetFocusedElement() as FrameworkElement;
    // verify focused control is a child of the ScrollViewer 
    if (scrollviewer != null && focusedElement != null && IsVisualChild(scrollviewer, focusedElement))
    {
        GeneralTransform focusedVisualTransform = focusedElement.TransformToVisual(scrollviewer);
        Rect rectangle = focusedVisualTransform.TransformBounds(new Rect(new Point(focusedElement.Margin.Left, focusedElement.Margin.Top), focusedElement.RenderSize));
        double newOffset = scrollviewer.VerticalOffset + (rectangle.Bottom - scrollviewer.ViewportHeight);
        scrollviewer.ScrollToVerticalOffset(newOffset);
    }
}

bool IsVisualChild(DependencyObject element, DependencyObject searchChildElement)
{
    if (element == searchChildElement)
        return true;
    // recursively process nested children in the visual tree 
    int children = VisualTreeHelper.GetChildrenCount(element);
    for (int i = 0; i < children; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(element, i);
        if (IsVisualChild(child, searchChildElement))
            return true;
    }
    return false;
}

private void txtbox6_GotFocus(object sender, RoutedEventArgs e)
{
    txtbox6.Focus();
    ScrollToFocused(sc);
} 

But Same Result... Your help is highly appreciated.

1
(App.Current as App).RootFrame.RenderTransform = new CompositeTransform(); in GotFocus method disables default scroll when you select a TextBox. Maybe this helps youKu6opr
@Ku6opr: This will not help me as it stops the whole page render transform. I Just want I cant set scroll of my scrollviewerArslan Pervaiz

1 Answers

0
votes

It is because your grid is limited in height.

Change Give some Height to your Grid by Height="600". May be 600 or more than 600.it depends your page.Try this.I can solve my problem using this Solution