列表视图可编程滚动在UWP

本文关键字:UWP 滚动 可编程 视图 列表 | 更新日期: 2023-09-27 18:12:38

我需要以编程的方式滚动ListView。实际上,我已经做了一部分,但效果很快,这与正常行为不同。我如何实现像listview正常垂直滚动行为?我只需要垂直滚动。在我的代码中,_scrollViewer是ListView的ScrollViewer, drawingPanel是一个Canvas组件,在ListView上。(Canvas应该在ListView的顶部,这是一个要求。)

顺便说一下,我的UI这边是这样的:
<grid>
   <listview>
   </listview>
   <canvas>
   </canvas>
</grid>

Code Side like That:

PointerPoint startPoint;      
        bool pressed = false;
        private void drawingPanel_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            pressed = true;
            startPoint = e.GetCurrentPoint(this.drawingPanel);         
        }
        private void drawingPanel_PointerMoved(object sender, PointerRoutedEventArgs e)
        {         
            PointerPoint endPoint = e.GetCurrentPoint(this.drawingPanel);
            PanListViewToNewPosition(startPoint, endPoint);
        }
        private void drawingPanel_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            pressed = false;           
            startPoint = null;
        }
        private void PanListViewToNewPosition(PointerPoint start, PointerPoint end)
    {
        if (pressed)
        {               
            double verticalOffset = start.Position.Y - end.Position.Y;              
            verticalOffset += this._scrollViewer.VerticalOffset;
            this._scrollViewer.ChangeView(null, verticalOffset, null);
        }
    }

列表视图可编程滚动在UWP

您确定需要程序化滚动吗?

似乎您所需要的只是使Canvas元素对指针(触摸、鼠标等)不可见,但对用户仍然可见。

做起来很简单。设置CanvasIsHitTestVisible属性为False:

<Canvas IsHitTestVisible="False">
...
</Canvas>

如果这样做,Canvas将在ListView的顶部,但不会滚动-它将是静态的。ListView将滚动。如果您希望CanvasListView的内容一起滚动,请将这两个元素放在ScrollViewer:

上。
<ScrollViewer>
   <Grid>
      <ListView>
      </ListView>
      <Canvas IsHitTestVisible="False">
      </Canvas>
   </Grid>
<ScrollViewer>