我需要以编程方式滚动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);
}
}
答案 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>