2012-04-25 143 views
0

給定的代碼正常工作與拖放控制的一個實例的用戶控件的多個實例。如果我嘗試再次丟棄相同的實例,則會拋出異常:拖放在WPF

指定的元素已經是另一個元素的邏輯子元素。先斷開它。

如何在Canvas上放置多個用戶控件實例,類似於Visual Studio工具箱的操作方法?

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); 
     } 
    } 
} 

在XAML代碼:

<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

您不能添加同一控制不同的容器 - 控制只能出現一次在視覺樹中。

而是預先加載用戶控件,你應該MyDesignerCanvas_Drop構建它們(即使用Activator你使用它,現在在LoadUsercontrols方法相同),並分配所產生的控制權交給UserControl.Content

+0

其實我使用的用戶控制對象在「TreeViewItem_PreviewMouseMove」事件這就是爲什麼我使用早期控制加載的克隆。如果可能的話請將部分代碼發給我。 – Sunil 2012-04-25 11:25:53

+0

如果你需要他們的多個實例,您無法加載控件月初。 – 2012-04-25 11:26:56

+0

感謝您的幫助,其現在的工作。 – Sunil 2012-04-25 13:59:28

0

我覺得你克隆控制_userControls[((System.Windows.Controls.HeaderedItemsControl)(treeItem)).Header.ToString()]MyDesignerCanvas_Drop

+0

請讓我知道我如何創建_userControls – Sunil 2012-04-25 11:27:57