我的 windows phone 7 应用程序中有一些页面,其中包含许多文本框。正如我们所知,windows phone 7 的默认行为是,当我们将焦点放在任何文本框上时,SIP 键盘会出现并向上滑动整个页面,并且在失去焦点时它会恢复到正常状态。我想在我的应用程序中限制这个东西。正如我在谷歌上搜索并找到的解决方案,我们将所有控件放入scrollviewer
并textbox
设置滚动查看器垂直偏移到该文本框位置的焦点。
现在我很困惑如何告诉滚动查看器我的垂直偏移控件的位置。
假设如果我可以设置那个东西,我需要为我的文本框
on GotFocus
和On LostFocus
. 有没有其他方法可以通过交互行为和触发器来调用这个东西。
更新...
<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);
}
但同样的结果......非常感谢您的帮助。