C# 显示SIP键盘上的“设置垂直偏移”
我的WindowsPhone7应用程序中有一些页面,其中有许多文本框。正如我们所知,WindowsPhone7有一个默认行为,即当我们关注任何文本框时,SIP键盘会弹出并向上滑动整个页面,当失去焦点时,它会恢复到正常状态。 我想在我的应用程序中限制这件事。正如我通过谷歌搜索发现的,解决方案是将所有控件放在C# 显示SIP键盘上的“设置垂直偏移”,c#,silverlight,windows-phone-7,xaml,expression-blend,C#,Silverlight,Windows Phone 7,Xaml,Expression Blend,我的WindowsPhone7应用程序中有一些页面,其中有许多文本框。正如我们所知,WindowsPhone7有一个默认行为,即当我们关注任何文本框时,SIP键盘会弹出并向上滑动整个页面,当失去焦点时,它会恢复到正常状态。 我想在我的应用程序中限制这件事。正如我通过谷歌搜索发现的,解决方案是将所有控件放在scrollviewer中,并将焦点放在textbox上,将scroll viewer Verical offset设置为该textbox的位置 现在我很困惑如何告诉scrollviewer垂直
scrollviewer
中,并将焦点放在textbox
上,将scroll viewer Verical offset设置为该textbox的位置
和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()作为FrameworkElement;
//验证聚焦控件是否为ScrollViewer的子级
if(scrollviewer!=null&&focusedElement!=null&&IsVisualChild(scrollviewer,focusedElement))
{
GeneralTransform focusedVisualTransform=focusedElement.TransformToVisual(scrollviewer);
Rect rectangle=focusedvisualttransform.TransformBounds(新Rect(新点(focusedElement.Margin.Left,focusedElement.Margin.Top),focusedElement.RenderSize));
double newOffset=scrollviewer.VerticalOffset+(rectangle.Bottom-scrollviewer.ViewportHeight);
scrollviewer.ScrollToVerticalOffset(新偏移量);
}
}
bool IsVisualChild(DependencyObject元素,DependencyObject搜索子元素)
{
if(元素==searchChildElement)
返回true;
//递归处理可视化树中的嵌套子级
int children=VisualTreeHelper.GetChildrenCount(元素);
for(int i=0;i
但同样的结果。。。
非常感谢你的帮助 这是因为网格的高度有限 改变
通过Height=“600”为网格增加一些高度。可能是600或超过600。这取决于您的页面。请尝试此操作。我可以使用此解决方案解决我的问题。
(App.Current as App)。RootFrame.RenderTransform=new compositeransform()当您选择文本框时,GotFocus
方法中的code>将禁用默认滚动。也许这有帮助you@Ku6opr:这对我没有帮助,因为它会停止整个页面渲染转换。我只是想我不能设置我的scrollviewer的滚动
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);
}