C# Winrt ListView是否通过代码滚动到任何项目?
我正在尝试滚动列表视图中的特定项目。C# Winrt ListView是否通过代码滚动到任何项目?,c#,xaml,listview,windows-runtime,windows-store-apps,C#,Xaml,Listview,Windows Runtime,Windows Store Apps,我正在尝试滚动列表视图中的特定项目。 列表视图包含500项(“问题”)。我想导航到特定的问题编号 xaml中的ListView <ListView x:Name="LstQuestions" SelectionMode="None" Grid.Row="1" d:IsLocked="True"> <ListView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid O
列表视图包含500项(“问题”)。我想导航到特定的问题编号 xaml中的ListView
<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]);
InitializeComponent();
对于(inti=1;i我不知道问题出在哪里。但我发现如果从事件调用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]);
}
公众问题()
{
初始化组件();
对于(inti=1;i我不知道问题出在哪里。但我发现如果从事件调用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]);
}
公众问题()
{
初始化组件();
for(int i=1;i是否在添加它们之后直接调用ScrollIntoView?在for循环之后是。在初始化组件之后将调用for循环。好的,然后尝试调用LstQuestion.UpdateLayout()在ScrollIntoView之前,我尝试了不起作用的方法。这对我起到了作用。添加它们后是否直接调用ScrollIntoView?在for循环之后是。在初始化组件后将调用for循环。好的,然后在ScrollIntoView之前尝试调用LstQuestion.UpdateLayout()。我尝试了不起作用的方法。这对我起到了作用。。