如何在UWP XAML / C#中通过Scroll Viewer更改滚动量?

时间:2017-08-20 07:04:26

标签: c# uwp uwp-xaml scrollviewer

我正在使用ListView来显示一些项目,比如高度100.如果有很多项目,垂直滚动条会显示ScrollViewer属性。但是,当用户滚动列表时,它有时会滚动屏幕的一半,因此只有一个滚动它覆盖整个列表视图。

我想要一些代码来设置滚动量,比如说当时的高度为100。 我尝试搜索doc但无法找到任何内容。实际上,数据来自于装订和物品数量的不同,但每件物品都有一些固定的高度。

示例代码:

<ListView Name="lvSummaryList" ScrollViewer.VerticalScrollMode="Enabled"
                                              ScrollViewer.IsVerticalRailEnabled="True"
                                              VerticalAlignment="Bottom"
                                              SelectionMode="None"                                          ScrollViewer.VerticalScrollBarVisibility="Auto"   
                                              ScrollViewer.HorizontalScrollMode="Disabled"              ScrollViewer.IsVerticalScrollChainingEnabled="True"
                                              Margin="0,5,10,0"
                                              MaxHeight="600" >
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <ItemsWrapGrid Orientation="Horizontal" GroupPadding="1"  Margin="1" MinHeight="100" MaxHeight="200" MaximumRowsOrColumns="4" VerticalAlignment="Center"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
 </ListView>

我如何实现这一目标,是否有财产甚至可以覆盖当前的行为?

2 个答案:

答案 0 :(得分:1)

您可以尝试设置SmallChange属性,也可以侦听垂直ScrollBar的Scroll事件,并根据需要实际调整ScrollBar位置。

PS:还有其他方法可以获得对ListView的滚动查看器的引用,如图所示。

    public MainPage()
    {
        this.InitializeComponent();

        var scrollViewer = GetDescendants(lvSummaryList).OfType<ScrollViewer>().FirstOrDefault();
        var verticalScrollbar = GetDescendants(scrollViewer).OfType<ScrollBar>()
            .FirstOrDefault(x => x.Orientation == Orientation.Vertical);

        verticalScrollbar.SmallChange = 5;

        //You can listen to the Scroll event of the vertical ScrollBar, and to pragmatically adjust the ScrollBar position
        /*verticalScrollbar.Scroll += (o, e) =>
        {
            if (e.ScrollEventType != ScrollEventType.EndScroll)
                scrollViewer.ScrollToVerticalOffset(100); // Scroll to the top

            if (e.NewValue >= verticalScrollbar.Maximum)
                scrollViewer.ScrollToVerticalOffset(0); // Scroll to the top
        };*/
    }

    public static IEnumerable<DependencyObject> GetDescendants(DependencyObject start)
    {
        var queue = new Queue<DependencyObject>();
        var count = VisualTreeHelper.GetChildrenCount(start);

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(start, i);
            yield return child;
            queue.Enqueue(child);
        }

        while (queue.Count > 0)
        {
            var parent = queue.Dequeue();
            var count2 = VisualTreeHelper.GetChildrenCount(parent);

            for (int i = 0; i < count2; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                yield return child;
                queue.Enqueue(child);
            }
        }
    }

答案 1 :(得分:1)

实现您想要实现的效果的更简单方法是在ScrollViewer上启用捕捉点。

使用以下作为ListView的样式:

        <Style TargetType="ListView"
               x:Key="SnapListViewStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListView">
                        <Border BorderBrush="{TemplateBinding BorderBrush}"
                                Background="{TemplateBinding Background}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <ScrollViewer x:Name="ScrollViewer"
                                          TabNavigation="{TemplateBinding TabNavigation}"
                                          HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                          HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                          IsHorizontalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsHorizontalScrollChainingEnabled}"
                                          VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                          VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                          IsVerticalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsVerticalScrollChainingEnabled}"
                                          IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                          IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                          VerticalSnapPointsAlignment="Near"
                                          VerticalSnapPointsType="MandatorySingle"
                                          ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}"
                                          IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                          BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}"
                                          AutomationProperties.AccessibilityView="Raw">
                                <ItemsPresenter Header="{TemplateBinding Header}"
                                                HeaderTemplate="{TemplateBinding HeaderTemplate}"
                                                HeaderTransitions="{TemplateBinding HeaderTransitions}"
                                                Footer="{TemplateBinding Footer}"
                                                FooterTemplate="{TemplateBinding FooterTemplate}"
                                                FooterTransitions="{TemplateBinding FooterTransitions}"
                                                Padding="{TemplateBinding Padding}" />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

如果这不能达到你想要的效果,请为VerticalSnapPointsTypeVerticalSnapPointsAlignment尝试不同的值。