ListBox ScrollIntoView无法正常工作OnNavigatedTo

时间:2016-01-03 17:50:39

标签: c# wpf xaml listbox

我正在制作一个可调节时间的计时器。出于这个原因,我想使用列表框设置时间(一个用于分钟,一个用于秒)。使用以下代码填充列表框。

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);
            }
        }
    }

我希望Listbox自动滚动到SelectedIndex,而是Listbox显示顶部的第一个Items。我怎样才能注意到ListBox滚动到SelectedIndex?

2 个答案:

答案 0 :(得分:1)

ListBox.ScrollIntoView()获取ListBox中的项目,而不是索引。 (MSDN documentation)所以而不是

listBoxobjM.ScrollIntoView(listBoxobjM.SelectedIndex);

listBoxobjM.ScrollIntoView(listBoxobjM.SelectedItem);

代替。

答案 1 :(得分:0)

最终我通过其他Stack Overflow question找到了解决方案。在这里,他们也遇到了ListView的问题,因为它充满了延迟。使用await Task.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);

            }
        }