WPF实现拖拽方法

第一种方式:利用WPF的thumb控件

 
       
           
       

       
           
           
       

       
   

 

后台代码

  private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Canvas.SetLeft(canvas1, Canvas.GetLeft(canvas1) + e.HorizontalChange);
            Canvas.SetTop(canvas1, Canvas.GetTop(canvas1) + e.VerticalChange);
        }

        private void thumb2_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Canvas.SetLeft(aaa, Canvas.GetLeft(aaa) + e.HorizontalChange);
            Canvas.SetTop(aaa,Canvas.GetTop(aaa)+e.VerticalChange);
        }

 

第二种方式

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfDragContrl
{
 ///
 /// Interaction logic for Window1.xaml
 ///
 public partial class Window1 : Window
 {
 Path connector = new Path();
 private PathGeometry connectorGeometry;
 private PathFigure connectorPoints;
 private BezierSegment connectorCurve;
 public Window1()
 {
 InitializeComponent();
 
 connector.Stroke = Brushes.Red;
 connector.StrokeThickness = 3;
 connectorGeometry = new PathGeometry();
 connectorPoints = new PathFigure();
 connectorCurve = new BezierSegment();
 SetBezierLine();
 connectorPoints.Segments.Add(connectorCurve);
 connectorGeometry.Figures.Add(connectorPoints);
 connector.Data = connectorGeometry;
 container.Children.Add(connector);
 }
 private void SetBezierLine()
 {
 connectorPoints.StartPoint =new Point((double)c1.GetValue(Canvas.LeftProperty) + c1.Width / 2,(double)c1.GetValue(Canvas.TopProperty) + c1.Height / 2);
 var endpoint= new Point((double)c2.GetValue(Canvas.LeftProperty) + c2.Width / 2,(double)c2.GetValue(Canvas.TopProperty) + c2.Height / 2);
 connectorCurve.Point1 = new Point(50,50);
 connectorCurve.Point2 = new Point(80,58);
 connectorCurve.Point3 = endpoint;
 }
 private void c_MouseMove(object sender, MouseEventArgs e)
 {
 if(!isdrag) return;
 var control = (sender as UIElement);
control.SetValue(Canvas.LeftProperty, e.GetPosition(container).X-control.DesiredSize.Width/2);
control.SetValue(Canvas.TopProperty, e.GetPosition(container).Y-control.DesiredSize.Height/2);
var vector= VisualTreeHelper.GetOffset(control);
this.Title=vector.X+"/"+vector.Y;
SetBezierLine();
}
private bool isdrag = false;
private void c_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isdrag = true;
}
private void c_MouseUp(object sender, MouseButtonEventArgs e)
{
isdrag = false;
}
private void DrawBezierLine(UIElement element1,UIElement element2)
{
}
}
}

 

 

你可能感兴趣的:(WPF实现拖拽方法)