实现拖放类似 [英] Implementing Drag and Drop similar

查看:90
本文介绍了实现拖放类似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[情况]

我有一个自定义的userControl(基本上是一个iamge,下面有一些文本)。在我的程序中,我添加了其中两个,并通过

I have a custom userControl (Basicly a iamge with some text beneath it). In my program I add two of these and implement a drag method by

public partial class CustomItem : UserControl
{
    private bool IsDragging { get; set; }
    private Point clickPosition;
    public CustomItem()
    {
        InitializeComponent();
        this.DataContext = this;
        this.MouseLeftButtonDown += (s, ea) =>
        {
            clickPosition = ea.GetPosition(this.LayoutRoot);
            this.CaptureMouse();
            IsDragging = true;
        };
        this.MouseMove += (s, ea) =>
        {
            if (IsDragging)
            {
                this.transFormThisShit.X = ea.GetPosition(this).X - clickPosition.X;
                this.transFormThisShit.Y = ea.GetPosition(this).Y - clickPosition.Y;
            }
        };
        this.MouseLeftButtonUp += (s, ea) =>
        {
            this.ReleaseMouseCapture();
            IsDragging = false;
        };
    }
}

[问题]

我可以在屏幕上自由移动对象,这正是我想要的。但是,当我将其中两个控件放到彼此之上时,我希望发生一个事件。什么都没有,但是以某种方式我似乎无法获得将其放到控件上的任何信息。

I can freely move around the objects over the screen which is exactly what I want. But when I drop 2 of these controls on top of eachother I wish to have a event happen. Anything at all, but somehow I can't seem to get any reading on the Control that I drop it on.

在上面有没有可能实现此目的的方法给定代码,这样当我将两个控件放到彼此的顶部时,我可以捕获一个可以同时获得两个控件的事件吗?

Is there any possible way to implement this in the above given code so that when I drop 2 of this control on top of eachother I can catch a / some event to which I can get both controls?

希望您能理解我的问题。

Hope you understand my question.

如果在当前情况下无法实现,请提示使用自定义控件的方式。

If not possible in current context please hint a way in which I can do it with custom controls..

也许有拖放控件之类的东西,我可以将其编辑为该控件一样!

Maybe there's something like a drag/drop control which I can "edit" into the same as this control is!

推荐答案

在MouseLeftButtonUp事件中,您可以使用检查控件拖动到的坐标并将其与控件进行比较您希望跌倒。我看不到其他明显的方法。

In the MouseLeftButtonUp event, you can use check the co-ordinates the control has been dragged to and compare these with the control you wish to drop onto. I can't see any other obvious way to do this.

有拖放框架,可用于对列表框中的项目进行重新排序,所以如果您发现自己'正在实施一些相当标准的东西,您不需要重新发明轮子。

There are drag and drop frameworks for things like re-ordering items in list boxes, so if you find you're implementing something pretty standard you shouldn't need to re-invent the wheel.

这篇关于实现拖放类似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆