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

  • Home
  • SEARCH
  • 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 7836757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:18:01+00:00 2026-06-02T14:18:01+00:00

The given code works fine with dragging and dropping one instance of control. If

  • 0

The given code works fine with dragging and dropping one instance of control. If I try to drop the same instance again it throws an exception:

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

How do I drop multiple instances of user controls on my Canvas, similar to how Visual Studio toolbox does?

public MainWindow()
{
    InitializeComponent();
    LoadUsercontrols();
}

private void LoadUsercontrols()
{
    List<string> userControlKeys = new List<string>();
    userControlKeys.Add("testCtrl1");
    userControlKeys.Add("testCtrl2");

    Type type = this.GetType();
    Assembly assembly = type.Assembly;
    foreach (string userControlKey in userControlKeys)
    {
        userControlFullName = String.Format("{0}.TestControls.{1}", type.Namespace, userControlKey);
        UserControl userControl = new UserControl();
        userControl = (UserControl)assembly.CreateInstance(userControlFullName);
        _userControls.Add(userControlKey, userControl);
    }
}

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

private void TreeViewItem_PreviewMouseMove(object sender, MouseEventArgs e)
{
    // Get the current mouse position
    System.Windows.Point mousePos = e.GetPosition(null);
    Vector diff = startPoint - mousePos;

    if (e.LeftButton == MouseButtonState.Pressed &&
     Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
     Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
    {
        TreeView treeView = sender as TreeView;
        TreeViewItem treeViewItem = FindAnchestor<TreeViewItem>((DependencyObject)e.OriginalSource);

        if (treeViewItem != null)
        {
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            DataObject dragData = new DataObject("myFormat", _userControls[((System.Windows.Controls.HeaderedItemsControl)(treeViewItem)).Header.ToString()]);
            DragDrop.DoDragDrop(treeViewItem, dragData, DragDropEffects.Copy);
        }
    }
}

private static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject
{
    do
    {
        if (current is T)
        {
            return (T)current;
        }
        current = VisualTreeHelper.GetParent(current);
    }
    while (current != null);
    return null;
}

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

private void MyDesignerCanvas_Drop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent("myFormat"))
    {
        if (treeItem != null)
        {
            UserControl myCanvasItem = e.Data.GetData("myFormat") as UserControl;
            UserControl newCanvastItem = new UserControl
            {
                Content = _userControls[((System.Windows.Controls.HeaderedItemsControl)(treeItem)).Header.ToString()]
            };
            Point position = e.GetPosition(MyDesignerCanvas);
            DesignerCanvas.SetLeft(newCanvastItem, position.X);
            DesignerCanvas.SetTop(newCanvastItem, position.Y);
            DesignerCanvas.SetZIndex(newCanvastItem, 1);
            MyDesignerCanvas.Children.Add(newCanvastItem);
        }
    }
}

In XAML Code:

<TreeView x:Name="presetTreeView4" Grid.Row="1" >
    <TreeViewItem Header="testCtrl1" Selected="TreeViewItem_Selected" PreviewMouseLeftButtonDown="TreeViewItem_PreviewMouseLeftButtonDown" PreviewMouseMove="TreeViewItem_PreviewMouseMove"/>
    <TreeViewItem Header="testCtrl2" Selected="TreeViewItem_Selected" PreviewMouseLeftButtonDown="TreeViewItem_PreviewMouseLeftButtonDown" PreviewMouseMove="TreeViewItem_PreviewMouseMove"/>
</TreeView>
<s:DesignerCanvas x:Name="MyDesignerCanvas" AllowDrop="True" Drop="MyDesignerCanvas_Drop" DragEnter="MyDesignerCanvas_DragEnter"  Background="#A6B0D2F5" DockPanel.Dock="Bottom" Margin="0" >
</s:DesignerCanvas>
  • 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-06-02T14:18:02+00:00Added an answer on June 2, 2026 at 2:18 pm

    You cannot add the same control to different containers – a control can only appear once in the visual tree.

    Instead of loading the user controls in advance, you should construct them at MyDesignerCanvas_Drop (i.e. use Activator the same way you’re using it right now in LoadUsercontrols) and assign the resulting control to the UserControl.Content.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code given below works fine. The only problem is mouse wheel/keyup cannot be
document.location() function given in the logn.js code works fine in Internet Explorer but does
I currently have the code below it works fine up until I actually try
This code works great for generating thumbnails, but when given a very large (100MB+)
I was given a c++ main and have to code it so it works.
The following code snippet works fine in 1.8.7 on Mac OS X, but not
Here's code that works fine: let f x y z = x + y
i got this exception when i done the code given below.. Unable to evaluate
Below code works fine in Firefox, but not in IE8. It triggers mouse click
I have a following code which works fine MsgBox(AddSomething(Of String)(Hello, World)) Public Function AddSomething(Of

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.