2022-2-17 触摸屏幕移动
#region 鼠标 private void canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e) { targetElement = Mouse.DirectlyOver as UIElement; if (targetElement != null) { targetPoint = e.GetPosition(NGEntry); } } private void canvas_PreviewMouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed && targetElement != null) { var pCanvas = e.GetPosition(borderName); //double gridtop = Convert.ToDouble(NGEntry.GetValue(Canvas.TopProperty)); //NGEntry.SetValue(Canvas.TopProperty, pCanvas.Y - targetPoint.Y); //Translate.Y = pCanvas.Y - targetPoint.Y; var y = pCanvas.Y - targetPoint.Y; Viewer.ScrollToVerticalOffset(Viewer.VerticalOffset - y); } } private void canvas_PreviewMouseUp(object sender, MouseButtonEventArgs e) { double gridtop = -Viewer.VerticalOffset; double gridh = NGEntry.ActualHeight; double viewh = Viewer.ActualHeight; if (gridtop > 0) { //NGEntry.SetValue(Canvas.TopProperty, 0d); //Translate.Y = 0d; Viewer.ScrollToVerticalOffset(0d); } if (viewh + (Math.Abs(gridtop)) > gridh) { //NGEntry.SetValue(Canvas.TopProperty, viewh - gridh); //Translate.Y = viewh - gridh; Viewer.ScrollToVerticalOffset(viewh - gridh); } } #endregion #region 触摸 private void canvas_TouchDown(object sender, TouchEventArgs e) { targetElement = Mouse.DirectlyOver as UIElement; if (targetElement != null) { targetPoint = e.GetTouchPoint(NGEntry).Position; } } private void canvas_TouchUp(object sender, TouchEventArgs e) { double gridtop = -Viewer.VerticalOffset; double gridh = NGEntry.ActualHeight; double viewh = Viewer.ActualHeight; if (gridtop > 0) { //NGEntry.SetValue(Canvas.TopProperty, 0d); //Translate.Y = 0d; Viewer.ScrollToVerticalOffset(0d); } if (viewh + (Math.Abs(gridtop)) > gridh) { //NGEntry.SetValue(Canvas.TopProperty, viewh - gridh); //Translate.Y = viewh - gridh; Viewer.ScrollToVerticalOffset(viewh - gridh); } } private void canvas_TouchMove(object sender, TouchEventArgs e) { if (targetElement != null) { var pCanvas = e.GetTouchPoint(borderName).Position; //double gridtop = Convert.ToDouble(NGEntry.GetValue(Canvas.TopProperty)); //NGEntry.SetValue(Canvas.TopProperty, pCanvas.Y - targetPoint.Y); //Translate.Y = pCanvas.Y - targetPoint.Y; var y = pCanvas.Y - targetPoint.Y; Viewer.ScrollToVerticalOffset(Viewer.VerticalOffset - y); } } #endregion