in WPF. How to scroll Objects in ScrollViewer by mouse-dragging, like as iPhone?

前端 未结 7 781
轻奢々
轻奢々 2020-12-30 10:59

it\'s done well to scroll by mouse-wheel or scrollbar seed-dragging. but scrolling by mouse-dragging contents on scroll view is not done. How can i implement this action?

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 11:57

    UWP variation:

        Pointer pointer;
        PointerPoint scrollMousePoint ;
        double hOff = 1;
    
        private void MainScrollviewer_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            pointer = e.Pointer;
            scrollMousePoint = e.GetCurrentPoint(scrollviewer);
            hOff = scrollviewer.HorizontalOffset;
            scrollviewer.CapturePointer(pointer);
        }
    
        private void MainScrollviewer_PointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            scrollviewer.ReleasePointerCaptures();
        }
    
        private void MainScrollviewer_PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            if (scrollviewer.PointerCaptures!= null&& scrollviewer.PointerCaptures.Count>0)
            {
              scrollviewer.ChangeView(hOff + (scrollMousePoint.Position.X - e.GetCurrentPoint(scrollviewer).Position.X),null,null);
            }
        }
    

    I know that question was for WPF, but this was best result I found searching for UWP solution.

提交回复
热议问题