【发布时间】:2016-01-03 17:50:38
【问题描述】:
我正在制作一个时间可调的计时器。出于这个原因,我想使用列表框设置时间(一个用于分钟,一个用于秒)。使用以下代码完成列表框的填充。
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?
【问题讨论】:
-
在输入 OnNavigatedTo 时检查您的列表框是否实际初始化/填充。
-
您可以通过将第二个
if设置为else if来改进您的代码(除非出于某种原因秒出排名分钟),我本来希望这两个条件都将break排除在外当你找到一个时循环(除非你正在寻找最后一个匹配,而不是第一个匹配)。很难说不知道你的代码做什么......只是基于你的代码的一般模式。