UWP中的ListView程序化滚动

时间:2016-10-11 14:31:11

标签: c# win-universal-app uwp windows-10-universal

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

顺便说一句,我的UI方面就是这样:

<grid>
   <listview>
   </listview>
   <canvas>
   </canvas>
</grid>

Code Side就像那样:

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

1 个答案:

答案 0 :(得分:0)

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

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

这很简单。将IsHitTestVisible上的Canvas属性设置为False

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

如果这样做,Canvas将位于ListView之上,但不会滚动 - 它将是静态的。 ListView会滚动。如果您希望Canvas滚动ListView的内容,请将这两个元素放在ScrollViewer上:

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