<ScrollViewer x:Name="scrollViewer" Grid.Row="1" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Visible"> <TextBlock xml:space="preserve" Grid.Row="1" Width="820" x:Name="TBContent" Style="{StaticResource ContentTextBlock}" TouchDown="TBContent_TouchDown" TouchMove="TBContent_TouchMove"/> </ScrollViewer>
前端用一个ScrollViewer 加文本 使用TouchDown 和 touchMove事件
后台事件代码:
double downYPosition = 0; //记录当前触摸位置 private void TBContent_TouchDown(object sender, System.Windows.Input.TouchEventArgs e) { var position = e.GetTouchPoint(this); downYPosition= position.Position.Y; } private void TBContent_TouchMove(object sender, System.Windows.Input.TouchEventArgs e) { var position = e.GetTouchPoint(this); var y = position.Position.Y; var disY = y - downYPosition; var offset = scrollViewer.VerticalOffset; var curOffset = offset - disY; if (curOffset <= 0) { curOffset = 0; } else if(curOffset> scrollViewer.ExtentHeight) { curOffset = scrollViewer.ExtentHeight; } scrollViewer.ScrollToVerticalOffset(curOffset); downYPosition = y; }