ListBox ScrollIntoView无法在NavigateTo上工作
本文关键字:NavigateTo 工作 ScrollIntoView ListBox | 更新日期: 2023-09-27 18:28:45
我正在制作一个时间可调的计时器。出于这个原因,我想使用列表框设置时间(一个用于分钟,一个用于秒)。列表框的填充是使用以下代码完成的。
public EditTime()
{
this.InitializeComponent();
List<Numbers> numbers = new List<Numbers>();
for (int i = 0; i < 61; i++)
{
numbers.Add(new Numbers() { Number = i });
}
listBoxobjM.ItemsSource = numbers;
listBoxobjS.ItemsSource = numbers;
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
}
public class Numbers
{
public int Number { get; set; }
}
在XAML中,我有以下ListBoxes代码:
<Grid Grid.Row="1" x:Name="ContentRootM" Margin="65,0,0,0" Width="130">
<ListBox Background="Transparent" Margin="10,10,10,10" BorderThickness="2" MaxHeight="580" Grid.Row="2" x:Name="listBoxobjM" VerticalContentAlignment="Top" HorizontalContentAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="5" BorderBrush="{ThemeResource ApplicationHeaderForegroundThemeBrush}" BorderThickness="1" Background="{ThemeResource ButtonBackgroundThemeBrush}">
<TextBlock TextAlignment="Left" HorizontalAlignment="Left" Width="70" Height="80" x:Name="LbM" Text="{Binding Number}" FontSize="48" Foreground="{ThemeResource ButtonForegroundThemeBrush}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<Grid Grid.Row="1" x:Name="ContentRootS" Margin="10,0,0,0" Width="130">
<ListBox Background="Transparent" Margin="10,10,10,10" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="2" x:Name="listBoxobjS" VerticalContentAlignment="Top" HorizontalContentAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="5" BorderBrush="{ThemeResource ApplicationHeaderForegroundThemeBrush}" BorderThickness="1" Background="{ThemeResource ButtonBackgroundThemeBrush}">
<StackPanel Width="125" Orientation="Horizontal" Grid.Row="1">
<TextBlock TextAlignment="Left" HorizontalAlignment="Left" Width="70" Height="80" x:Name="LbS" Text="{Binding Number}" TextWrapping="Wrap" FontSize="48" Foreground="{ThemeResource ButtonForegroundThemeBrush}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
当我导航到此页面时,我可以使用listBoxobjM.SelectedIndex=I选择预选时间(分钟和秒);在以下代码中。我可以手动滚动到此项目,并看到它已被选中。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
string parameter = e.Parameter as string;
string[] parts = parameter.Split(';');
id = int.Parse(parts[0]);
....
DatabaseHelper fetch = new DatabaseHelper();
TeamType team = fetch.GetTeamType(id);
for (int i = 0; i < 60; i++)
{
if (i == team.PlayTime.Minutes)
{
listBoxobjM.SelectedIndex = i;
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedIndex);
}
if (i == team.PlayTime.Seconds)
{
listBoxobjS.SelectedIndex = i;
listBoxobjS.ScrollIntoView(listBoxobjS.SelectedIndex);
}
}
}
我希望列表框自动滚动到SelectedIndex,但列表框显示顶部的第一个项目。如何确保ListBox滚动到SelectedIndex?
ListBox.ScrollIntoView()
接受ListBox中的项,而不是索引。(MSDN文档)所以不是
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedIndex);
尝试
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedItem);
相反。
最终,我通过另一个Stack Overflow问题自己找到了解决方案。在这里,他们也遇到了ListView的问题,因为它充满了延迟。使用awaitTask.Delay(请参阅代码)可以解决问题。我感谢大家的贡献。
for (int i = 0; i < 60; i++)
{
if (i == team.PlayTime.Minutes)
{
listBoxobjM.SelectedIndex = i;
await Task.Delay(1000);
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedItem);
}
if (i == team.PlayTime.Seconds)
{
listBoxobjS.SelectedIndex = i;
await Task.Delay(1000);
listBoxobjS.ScrollIntoView(listBoxobjS.SelectedItem);
}
}