Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:27:52+00:00 2026-05-29T22:27:52+00:00

I have a Canvas that contains a button which I want to be able

  • 0

I have a Canvas that contains a button which I want to be able to drag and drop into another canvas. I want to copy the button to the other Canvas. Here is the code I am using:

The XAML:

<Window>
    <Grid>
        <Canvas
            Height="300"
            Width="500"
            Background="Gray">
            <Canvas
                Name="cnvToolBox"
                Canvas.Left="10"
                Canvas.Top="10"
                Background="AliceBlue"
                Width="100"
                Height="200">
                <Button
                    Content="Drag Me!"
                    PreviewMouseLeftButtonDown="Button_PreviewMouseLeftButtonDown"
                    PreviewMouseMove="Button_PreviewMouseMove"></Button>
            </Canvas>
            <Rectangle 
                Canvas.Left="119"
                Canvas.Top="9"
                Width="102"
                Height="202"
                StrokeDashArray="0.5 1.0 0.3" 
                Stroke="Black" 
                StrokeThickness="2"/>
            <Canvas    
                Name="cnvButtonDropZone"
                Canvas.Left="120"
                Canvas.Top="10"
                Width="100"
                Height="200"
                Background="LightGreen"
                AllowDrop="True"
                DragEnter="Canvas_DragEnter"
                Drop="Canvas_Drop">
            </Canvas>
        </Canvas>
    </Grid> 
</Window>

Here’s the Code Behind:

public partial class MainWindow : Window
{
    private Point startPoint;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startPoint = e.GetPosition(null);
    }

    private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        Point currentPosition = e.GetPosition(null);
        Vector diff = startPoint - currentPosition;

        if (e.LeftButton == MouseButtonState.Pressed &&
            (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
        {
            Button button = sender as Button;

            DataObject dragData = new DataObject("myFormat", button);
            DragDrop.DoDragDrop(button, dragData, DragDropEffects.Copy);
        }
    }

    private void Canvas_DragEnter(object sender, DragEventArgs e)
    {
        if (!e.Data.GetDataPresent("myFormat") || sender == e.Source)
        {
            e.Effects = DragDropEffects.None;
        }
    }

    private void Canvas_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("myFormat"))
        {
            Button button = e.Data.GetData("myFormat") as Button;

            Canvas canvas = sender as Canvas;
            canvas.Children.Add(button);

        }
    }
}

When I drop the button I get the following exception when I’m adding the button to the canvas:

Specified element is already the logical child of another element. Disconnect it first.

I’m just trying to learn how to drag and drop controls and not really sure what that error means and how to resolve it. I don’t know where I’m going wrong. Any suggestions would be welcome.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:27:53+00:00Added an answer on May 29, 2026 at 10:27 pm

    The button is owned by its parent cnvToolBox. You need to remove it from cnvToolBox before adding it to the canvas.

                cnvToolBox.Children.Remove(button);
                var canvas = sender as Canvas;
                canvas.Children.Add(button);
    

    This moves the button from your toolbox to the canvas. If you actually want to clone the item you want something like:

            if (e.Data.GetDataPresent("myFormat"))
            {
                var contentControl = (ContentControl)e.Data.GetData("myFormat");
    
                var constructorInfo = contentControl.GetType().GetConstructor(new Type[] {});
                if (constructorInfo != null)
                {
                    var newElement = (UIElement)constructorInfo.Invoke(new object[]{});
    
                    var newContentControl = newElement as ContentControl;
                    if(newContentControl != null)
                    {
                        newContentControl.Content = contentControl.Content;
                    }
    
                    ((Panel)sender).Children.Add(newElement);
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scroll viewer in my application that contains a canvas, in which
I have a shell that contains a canvas, which in turn draws some text
I have a canvas that contains a couple other elements. I need this canvas
I have a canvas which contains specific usercontrols. At some point, I want to
I have a canvas in Flex that shall be able only to be scrolled
I have a Canvas in a Flex application which has items inside it that
I have a Silverlight application which has on it a Canvas. On that Canvas,
In my WPF application, I have a Canvas object that contains some UserControl objects.
I have a Canvas which contains a few Textblocks and I need to find
I have a form that contains a tab canvas. On the second canvas page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.