【问题标题】:ListBox ScrollIntoView not working OnNavigatedToListBox ScrollIntoView 不起作用 OnNavigatedTo
【发布时间】: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 排除在外当你找到一个时循环(除非你正在寻找最后一个匹配,而不是第一个匹配)。很难说不知道你的代码做什么......只是基于你的代码的一般模式。

标签: c# wpf xaml listbox


【解决方案1】:

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

listBoxobjM.ScrollIntoView(listBoxobjM.SelectedIndex);

试试

listBoxobjM.ScrollIntoView(listBoxobjM.SelectedItem);

改为。

【讨论】:

  • 1.列表框填充了正确的数据:选择了正确的数字。
  • 我使用了两次 if 语句,因为我希望以适当的值同时选择 Seconds 和 Minutes。就这样。使用 SelectedItem 代替 SelectedIndex 我已经尝试过了,但结果还是一样。
【解决方案2】:

最终我通过另一个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);

            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2020-01-09
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多