在WPF中模拟拖放事件

在WPF中模拟拖放事件,wpf,drag-and-drop,Wpf,Drag And Drop,我想在WPF中模拟拖放事件。 为此,我需要访问存储在“拖放缓冲区”中的数据,还需要创建一个DragEventArgs。 我注意到,DragEventArgs是密封的,没有公共密码 所以我的问题是: 1.如何创建DragEventArgs的实例? 2.我如何才能访问拖放缓冲区?我最近这样做了!我用MouseDown、MouseMove和MouseUp事件模拟拖放。例如,对于我的应用程序,我有一些画布,我想拖放它们。每个画布都有一个id。在MouseDown事件中,我缓冲其id并在MouseMove

我想在WPF中模拟拖放事件。
为此,我需要访问存储在“拖放缓冲区”中的数据,还需要创建一个
DragEventArgs
。 我注意到,
DragEventArgs
是密封的,没有公共密码

所以我的问题是:
1.如何创建
DragEventArgs
的实例?

2.我如何才能访问拖放缓冲区?

我最近这样做了!我用MouseDown、MouseMove和MouseUp事件模拟拖放。例如,对于我的应用程序,我有一些画布,我想拖放它们。每个画布都有一个id。在MouseDown事件中,我缓冲其id并在MouseMove和MouseUp事件中使用它。桌面画布是我的主画布,其中包含一些画布。这些画布在我的字典里

这是我的密码:

private Dictionary<int, Win> dic = new Dictionary<int, Win>();
    private Point downPoint_Drag = new Point(-1, -1);
    private int id_Drag = -1;
    private bool flag_Drag = false;

    public class Win
    {
        public Canvas canvas = new Canvas();
        public Point downpoint = new Point();

        public Win()
        {
            canvas.Background = new SolidColorBrush(Colors.Gray);
        }
    }

    private void Desktop_Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        try
        {
            Point movePoint = e.GetPosition(Desktop_Canvas);

                    if (flag_Drag && downPoint_Drag != new Point(-1, -1))
                    {
                        double dy1 = movePoint.Y - downPoint_Drag.Y, x = -1, dx1 = movePoint.X - downPoint_Drag.X, y = -1;
                        downPoint_Drag = movePoint;
                        if (x == -1)
                            x = Canvas.GetLeft(dic[id_Drag].canvas) + dx1;
                        if (y == -1)
                            y = Canvas.GetTop(dic[id_Drag].canvas) + dy1;
                        Canvas.SetLeft(dic[id_Drag].canvas, x);
                        Canvas.SetTop(dic[id_Drag].canvas, y);
                    }
        }
        catch
        {
            MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
            Desktop_Canvas_MouseLeave(null, ee);
        }
    }

    private void Desktop_Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        try
        {
  downPoint_Drag  = new Point(-1, -1);
            id_Drag =-1;
            flag_Drag = false;
        }
        catch
        {
            MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
            Desktop_Canvas_MouseLeave(null, ee);
        }
    }

    private void Desktop_Canvas_MouseLeave(object sender, MouseEventArgs e)
    {
        MouseButtonEventArgs ee = new MouseButtonEventArgs((MouseDevice)e.Device, 10, MouseButton.Left);
            Desktop_Canvas_MouseLeftButtonUp(null, ee);
    }

    void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {            
        downPoint_Drag = e.GetPosition(Desktop_Canvas);
        int hoverId = HoverWin(downPoint_Drag);
        flag_Drag = true;
        id_Drag = hoverId;
        dic[id_Drag].downpoint = new Point(downPoint_Drag.X, downPoint_Drag.Y);
    }

    private int HoverWin(Point p)
    {
        foreach (int i in dic.Keys)
        {
                if (dic[i].canvas.IsMouseOver)
                    return i;
        }
        return -1;
    }
private Dictionary dic=new Dictionary();
专用点下降点=新点(-1,-1);
私有int id_Drag=-1;
私有布尔标志_Drag=false;
公众阶级的胜利
{
公共画布=新画布();
公共点下降点=新点();
公共赢()
{
canvas.Background=新的SolidColorBrush(Colors.Gray);
}
}
专用void桌面\u画布\u鼠标移动(对象发送器,鼠标目标e)
{
尝试
{
点移动点=e.GetPosition(桌面画布);
如果(标志拖动和下降点拖动!=新点(-1,-1))
{
double dy1=movePoint.Y-downPoint\u Drag.Y,x=-1,dx1=movePoint.x-downPoint\u Drag.x,Y=-1;
下降点阻力=移动点;
如果(x==-1)
x=Canvas.GetLeft(dic[id\u Drag].Canvas)+dx1;
如果(y==-1)
y=Canvas.GetTop(dic[id\u Drag].Canvas)+dy1;
Canvas.SetLeft(dic[id\u Drag].Canvas,x);
Canvas.SetTop(dic[id\u Drag].Canvas,y);
}
}
抓住
{
MouseEventArgs ee=新的MouseEventArgs((MouseDevice)e.装置,10);
桌面、画布和鼠标(空、ee);
}
}
private void Desktop_Canvas_MouseLeftButtonUp(对象发送器,MouseButtonEventArgs e)
{
尝试
{
下降点阻力=新点(-1,-1);
id_阻力=-1;
flag_Drag=false;
}
抓住
{
MouseEventArgs ee=新的MouseEventArgs((MouseDevice)e.装置,10);
桌面、画布和鼠标(空、ee);
}
}
专用void Desktop_Canvas_MouseLeave(对象发送器,MouseEventArgs e)
{
MouseButtonEventArgs ee=新的MouseButtonEventArgs((MouseDevice)e.Device,10,MouseButton.Left);
桌面\画布\鼠标左键按钮(空,ee);
}
void canvas_MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e)
{            
下降点拖动=e.GetPosition(桌面画布);
int hoverId=HoverWin(下降点阻力);
flag_Drag=true;
id_Drag=hoverId;
dic[id\u Drag]。下降点=新点(下降点\u Drag.X,下降点\u Drag.Y);
}
私人int HoverWin(p点)
{
foreach(dic.键中的int i)
{
if(dic[i].canvas.IsMouseOver)
返回i;
}
返回-1;
}