2012-07-15 26 views
1

我的windows phone 7应用程序中有一些页面,其中有很多文本框。正如我们所知,Windows Phone 7的默认行为是,当我们将注意力集中在任何文本框上时,SIP键盘会弹出并滑动整个页面,失去焦点时会恢复到正常状态。 我想在我的应用程序中限制这件事。由于我已经搜索并找到了解决方案,我们将所有控件放在scrollviewer中,并在textbox的焦点上将滚动查看器的真实偏移量设置为该文本框的位置。在SIP键盘上出现设置垂直偏移量

  1. 现在我很困惑我该如何告诉scrollviewer关于我的垂直偏移控件的位置。

  2. 并假设如果我可以设置那个东西,我需要调用两个方法来处理我的文本框on GotFocusOn LostFocus。有没有其他方法可以通过交互行为和触发器来调用这个东西?

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> 

和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); 
} 

但相同的结果... 你的帮助是高度赞赏。

+0

'(App.Current as App).RootFrame.RenderTransform = new CompositeTransform();'in GotFocus'方法在您选择“文本框”时禁用默认滚动。也许这可以帮助你 – Ku6opr 2012-07-15 06:35:52

+0

@ Ku6opr:这不会帮助我,因为它会停止整个页面渲染转换。我只想我不能设置我的scrollviewer的滚动 – 2012-07-15 07:47:44

回答

0

这是因为你的网格高度有限。

更改 通过Height =“600”为网格添加一些高度。可能600或600以上。这取决于你的网页。试试这个。我可以使用这个解决方案解决我的问题。