开发者

How to know if you're Dropping in the same app instance where you made the Drag in WPF?

开发者 https://www.devze.com 2023-03-25 22:19 出处:网络
the title pretty much explains my issue: I am right now taking care of drag & drop in my app. I can have many instances of my app running at the same time, and I can drag from one instance to the

the title pretty much explains my issue:

I am right now taking care of drag & drop in my app. I can have many instances of my app running at the same time, and I can drag from one instance to the other without trouble.

Now, I would like to know if I'm drag & dropping "internally" (i.e: the drop occurs in the same instance as the drag) or 开发者_JAVA技巧"externally" (the opposite)

I went this far: I need to add to my dragged data a unique ID (something like a PUID) that identifies the app where I'm making the drag. Then I can just compare this id to the one I have locally on the drop and see if it is the same.

I have no problem transferring such info in my drag Data, the issue is more to find this UId.

I have been thinking using the Process.GetCurrentProcess().MainWindowHandle; but I'm not sure if this is a good idea.

What option(s) do I have to make this work?


I would simply create a readonly Guid that gets set when you start your app.

You can put this wherever your main logic lives (MainWindow or ViewModel).

Here is a snippet:

public class MyViewModel
{
    private readonly Guid mUID = Guid.NewGuid();

    // In case you want a property for it
    public string UniqueApplicationID
    {
        get { return mUID; }
    }

    public void OnDropHandler(MyViewModel objectBeingDropped)
    {
        if (objectBeingDropped.UniqueApplicationID == mUID)
            return;

       // Handle drop normally here
    }
}


The D-n-D is much like an UI activity, than an internal one.

I would distinguish two contexts: dropping a file, and dropping some object (e.g. VS designer). In the first context there's no problem at all, because it doesn't matter where you pull the data. In the second case, you should know what object has been chosen. For instance, you have a listbox with many items (e.g. the alphabet chars), once the user D-n-D any of those items, the internal operation is a simple reference to the selected object. By pulling the data from another app, you won't be able to find your object, because the source is different.

In case of structs or strings, you may wrap them with a GUID, as you correctly proposed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号