简体   繁体   English

如何在UWP中自动滚动ListView?

[英]How to auto scroll ListView in UWP?

I am creating a UWP for my Raspberry Pi. 我正在为Raspberry Pi创建UWP。 My app displays some data to the screen using a ListView . 我的应用程序使用ListView在屏幕上显示一些数据。 But, the screen is not connected to the mouse or keyboard, so I can't scroll to see the whole data. 但是,屏幕未连接到鼠标或键盘,因此无法滚动查看全部数据。 Is there any way I can auto-scroll the list view and when it reaches the end, it goes to the beginning of the list again? 有什么方法可以自动滚动列表视图,当列表视图到达末尾时,它将再次转到列表的开头?

<local:AlternatingRowListView>
    <local:AlternatingRowListView.ItemTemplate>
        <DataTemplate>
            <Grid Width="{Binding ActualWidth, ElementName=TableData}" Padding="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1.5*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="3*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding UpperDescription}" FontSize="24" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Margin="2" HorizontalAlignment="Left" Foreground="Black"/>
                <TextBlock Text="{Binding Title}" FontSize="76" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" Margin="2" HorizontalAlignment="Left" Foreground="Black"/>
                <TextBlock Text="{Binding LowerDescription}" FontSize="24" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" Margin="2"  HorizontalAlignment="Left" Foreground="Black"/>
                <!--<TextBlock Text="{Binding Deadline}" FontSize="24" Grid.Row="1" Grid.Column="3" Margin="2"  HorizontalAlignment="Center" Foreground="Black"/>
                <TextBlock Text="{Binding State}" FontSize="24" Grid.Row="1" Grid.Column="4" Margin="2"  HorizontalAlignment="Center" Foreground="Black"/>-->
            </Grid>
        </DataTemplate>
    </local:AlternatingRowListView.ItemTemplate>
</local:AlternatingRowListView>

If you want to scroll to a specific item in the list, you can use ScrollToView method: 如果要滚动到列表中的特定项目,可以使用ScrollToView方法:

listView.ScrollIntoView(item);

If you need more granular control (like listening to the embedded ScrollViewer events) you first need to get access to the embedded control: 如果您需要更精细的控件(例如侦听嵌入式ScrollViewer事件),则首先需要访问嵌入式控件:

The following helper method will return the first ScrollViewer within a given DependencyObject : 以下帮助器方法将返回给定DependencyObject的第一个ScrollViewer

public static ScrollViewer GetScrollViewer(DependencyObject dependencyObject)
{
    if (dependencyObject is ScrollViewer scroller) return scroller;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
    {
        var child = VisualTreeHelper.GetChild(dependencyObject, i);

        var result = GetScrollViewer(child);
        if (result != null) return result;
    }
    return null;
}

So now you can access the ScrollViewer like this: 因此,现在您可以像这样访问ScrollViewer

var scrollViewer = GetScrollViewer(listView);

Is there any way I can auto-scroll the list view and when it reaches the end, it goes to the beginning of the list again? 有什么方法可以自动滚动列表视图,当列表视图到达末尾时,它将再次转到列表的开头?

I noted that you're making a custom AlternatingRowListView by yourself, it's a good start. 我注意到您自己创建了一个自定义的AlternatingRowListView ,这是一个好的开始。 If you want to make your ListView auto-scroll, you could try to add a timer in your custom AlternatingRowListView . 如果要使ListView自动滚动,可以尝试在自定义AlternatingRowListView添加计时器。 For example, the DispatcherTimer , you could start the timer when it's loaded. 例如, DispatcherTimer ,您可以在加载计时器时启动它。 Then, in its Tick event handler, you could use the ScrollViewer to scroll the list. 然后,在其Tick事件处理程序中,可以使用ScrollViewer滚动列表。

About how to get the ScrollViewer? 关于如何获取ScrollViewer? I believe you've overrided the OnApplyTemplate method in your custom control class, you could call this.GetTemplateChild("ScrollViewer") as ScrollViewer to get the ScrollViwer in it. 我相信您已经在自定义控件类中重写了OnApplyTemplate方法,可以将this.GetTemplateChild("ScrollViewer") as ScrollViewer来获取ScrollViwer。

You could refer to the Bottom-up list (XAML) sample for details. 您可以参考自下而上列表(XAML)示例以获取详细信息。

Then, you need to manually calculate if it reaches the end. 然后,您需要手动计算它是否到达终点。 Once it reaches the end, you could stop the timer and scroll to the top of the list. 一旦到达终点,您可以停止计时器并滚动到列表顶部。 The above sample also involves part of the calculation, you could check it. 上述示例还涉及部分计算,您可以检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM