0

How do I make my Custom controls draggable and droppable on a grid let's say?

I want to drag a panel(custom control) and drop it somewhere on my screen, in the best case in a grid, for example how it's done in Visual Studio, you can grab the solution explorer let's say and drop it somewhere, but how exactly do I do that?

  • Possible duplicate of [C# WPF - Resizable Usercontrol / Page (Grab and drag resizing)](https://stackoverflow.com/questions/53350970/c-sharp-wpf-resizable-usercontrol-page-grab-and-drag-resizing) – ASh Nov 18 '18 at 07:18
  • No, it is not a duplicate. The one is for resizable panels, and this one is for garbbing panels and dropping them in a grid – Krusto Stoianov Nov 18 '18 at 10:42

2 Answers2

0

You need to build your project and then it will be automatically available in the Toolbox when you are in the XAML designer. Just like the common controls.

For Drag and Drop at runtime look and the official WPF documentation. Also I suggest you look at the GongSolutions.WPF.DragDrop library in GitHub it is open source so you can see how they implemented it if the functionality it provides does not do what you want.

Aleš Doganoc
  • 11,568
  • 24
  • 40
  • I mean something else. I want to be able to grab the custom control from my window and move it, and in the best case be able to drop it in another grid – Krusto Stoianov Nov 18 '18 at 10:39
0

I found this post with a very similar conclusion to this one. the difference being scale.

the following example will swap the parent container

    int i = 0;
    void swapLocations()
    {

        foreach(var formObject in objList) //objList == a list or array on all objects you want to move from one container to another
        {

            if (i % 2 == 0)
            {

                // catch current position             
                Point moveLocation = new Point(formObject.Location.X + formObject.Parent.Location.X,formObject.Location.Y + formObject.Parent.Location.Y);

                // remove this object
                formObject.Parent.Controls.Remove(formObject);

                // add this object to the form
                this.Controls.Add(formObject);

                 // set location
                formObject.Location = moveLocation;

                formObject.SendToBack();
            }
            else
            {
                formObject.BringToFront();
            }
        }
        ++i;
    }