Winrt ListView 通过代码滚动到任何项目?
Winrt ListView Scroll To any Item via code?
我正在尝试滚动到 ListView 中的特定项目。
ListView 包含 500 个项目 ("Questions")。我想将其导航到特定问题编号。
xaml
中的列表视图
<ListView x:Name="LstQuestions" SelectionMode="None" Grid.Row="1" d:IsLocked="True">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
ListView 项是从代码加载的。
InitializeComponent();
for(int i=1;i<= 500; i++)
{
Button btn = new Button();
btn.Width = 120;
btn.Height = 120;
btn.Click += Btn_Click;
btn.Content = i;
LstQuestions.Items.Add(btn);
}
LstQuestions.UpdateLayout();
LstQuestions.ScrollIntoView((Button)LstQuestions.Items[_CurrentQuestionNumber]);
我正在尝试的是我有一个存储当前问题编号的设置,我希望它自动滚动到 ListView 中的那个问题编号。
我尝试了 ListView.ScrollIntoView 和 ListView.MakeVisible 方法。没有任何效果。
我还发现了一件事情。上面的代码在 MainPage 中运行良好。但是,如果我导航
Frame.Navigate(typeof(Questions))
那么上面的代码将不会在问题页面中工作。
如何实现?
我不知道问题出在哪里。但我发现,如果您从事件中调用 ScrollIntoView,它将起作用。
所以我创建了 Layout_Updated 事件。从该事件调用了 ScrollIntoView。
<ListView x:Name="LstQuestions" SelectionMode="None" Grid.Row="1" LayoutUpdated="LstQuestions_LayoutUpdated" d:IsLocked="True">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
public Questions()
{
InitializeComponent();
for(int i=1;i<= 500; i++)
{
Button btn = new Button();
btn.Width = 120;
btn.Height = 120;
btn.Click += Btn_Click;
btn.Content = i;
LstQuestions.Items.Add(btn);
}
LstQuestions.UpdateLayout();
}
private void LstQuestions_LayoutUpdated(object sender, object e)
{
LstQuestions.ScrollIntoView(LstQuestions.Items[_CurrentQuestionNumber]);
}
现在可以了。
我正在尝试滚动到 ListView 中的特定项目。
ListView 包含 500 个项目 ("Questions")。我想将其导航到特定问题编号。
xaml
中的列表视图<ListView x:Name="LstQuestions" SelectionMode="None" Grid.Row="1" d:IsLocked="True">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
ListView 项是从代码加载的。
InitializeComponent();
for(int i=1;i<= 500; i++)
{
Button btn = new Button();
btn.Width = 120;
btn.Height = 120;
btn.Click += Btn_Click;
btn.Content = i;
LstQuestions.Items.Add(btn);
}
LstQuestions.UpdateLayout();
LstQuestions.ScrollIntoView((Button)LstQuestions.Items[_CurrentQuestionNumber]);
我正在尝试的是我有一个存储当前问题编号的设置,我希望它自动滚动到 ListView 中的那个问题编号。
我尝试了 ListView.ScrollIntoView 和 ListView.MakeVisible 方法。没有任何效果。
我还发现了一件事情。上面的代码在 MainPage 中运行良好。但是,如果我导航
Frame.Navigate(typeof(Questions))
那么上面的代码将不会在问题页面中工作。
如何实现?
我不知道问题出在哪里。但我发现,如果您从事件中调用 ScrollIntoView,它将起作用。
所以我创建了 Layout_Updated 事件。从该事件调用了 ScrollIntoView。
<ListView x:Name="LstQuestions" SelectionMode="None" Grid.Row="1" LayoutUpdated="LstQuestions_LayoutUpdated" d:IsLocked="True">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
public Questions()
{
InitializeComponent();
for(int i=1;i<= 500; i++)
{
Button btn = new Button();
btn.Width = 120;
btn.Height = 120;
btn.Click += Btn_Click;
btn.Content = i;
LstQuestions.Items.Add(btn);
}
LstQuestions.UpdateLayout();
}
private void LstQuestions_LayoutUpdated(object sender, object e)
{
LstQuestions.ScrollIntoView(LstQuestions.Items[_CurrentQuestionNumber]);
}
现在可以了。