我有一个带有一组垂直文本框的页面。如果其中一个被聚焦,则所有这些都应该可见,即使显示屏幕键盘也是如此。它们的数量足以让它们全部放入键盘上方的可用空间中。当底部文本框获得焦点时,页面会自动向上滚动,以便所有这些都可见,但如果顶部文本框获得焦点,屏幕键盘会覆盖底部。
这是我的页面的简化示例:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ItemsControl ItemsSource="{Binding List}" Margin="120 140 0 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0 10 0 0">
<TextBox Text="{Binding Text, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
DataContext
包含 10 个项目的列表:
public class Item
{
public string Text { get; set; }
}
public class ViewModel
{
public List<Item> List { get; set; }
}
public MainPage()
{
this.InitializeComponent();
DataContext = new ViewModel
{
List = Enumerable.Range(0, 10).Select(i => new Item { Text = i.ToString() }).ToList()
};
}
我已经尝试了几种方法,但都没有成功:
- 在
TextBox.GotFocus
事件中以编程方式将焦点更改为底部文本框并返回。 - In
TextBox.GotFocus
event andInputPane.Showing
event tried setting the vertical offset of aScrollViewer
: (a) the one I included in the page around theGrid
(b) the one in the visual tree above thePage
that Windows uses to automatically bring the focused control in view. In both cases theScrollViewer
doesn't react toScrollToVerticalOffset
calls.
I've also looked at the sample suggested in this question but it reacts to onscreen keyboard differently, not by scrolling the page.