1: #region MouseLeftButtonUp
2: void objFrameworkElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
3: {
4: //Stop Drag
5: FrameworkElement objFrameworkElement = (FrameworkElement)sender;
6: objFrameworkElement.ReleaseMouseCapture();
7:
8: objFrameworkElement.MouseMove -=
9: new MouseEventHandler(objFrameworkElement_MouseMove);
10: objFrameworkElement.MouseLeftButtonUp -=
11: new MouseButtonEventHandler(objFrameworkElement_MouseLeftButtonUp);
12:
13: // If it is an element marked [draggable]
14: // try to drop it on a panel stored in the colPanels collection
15: if (objFrameworkElement.Tag.ToString().Contains("[draggable]"))
16: {
17: Point tmpPoint = e.GetPosition(null);
18: // Build a list of elements at the current mouse position
19: List hits = (List)this.HitTest(tmpPoint);
20: // Loop through all the Panels in the colPanels collection
21: foreach (Panel objPanel in colPanels)
22: {
23: if (hits.Contains(objPanel))
24: {
25: // Grab the position of the element being dragged in relation to it's position on the
26: // main canvas and its position in relation to the panel it may be dropped on
27: Point mousePos1 = e.GetPosition(objPanel);
28: Point mousePos2 = e.GetPosition(objFrameworkElement);
29:
30: // Remove the element from the main canvas
31: this.LayoutRoot.Children.Remove(objFrameworkElement);
32:
33: // Import content
34: // Get a reference to the parent of the current panel
35: UserControl objUserControl = (UserControl)objPanel.Parent;
36: // See if that parent implements an interface called "ImportContent"
37: object objObject = objUserControl.GetType().GetInterface("ImportContent", true);
38:
39: // If the object is not null then the parent object has a method called "ImportContent"
40: if (!(objObject == null))
41: {
42: // Create a parmeters array
43: object[] parameters = new object[1];
44: // Add the elemnt that is being dragged to the array
45: parameters.SetValue(objFrameworkElement, 0);
46: // invoke the "ImportContent" on the parent object passing the parameters array that
47: // contains the element being dragged
48: bool boolImport = (bool)objUserControl.GetType().InvokeMember("ImportContent",
49: BindingFlags.InvokeMethod, null, objUserControl, parameters);
50:
51: // If the import was not successful simply add the element to the panel
52: if (!boolImport)
53: {
54: // Add the element to the panel
55: objPanel.Children.Add(objFrameworkElement);
56: Canvas.SetLeft(objFrameworkElement, mousePos1.X - mousePos2.X);
57: Canvas.SetTop(objFrameworkElement, mousePos1.Y - mousePos2.Y);
58: }
59: }
60: else
61: {
62: // The parent object does not implement the "ImportContent" Interface
63: // Add the element to the panel
64: objPanel.Children.Add(objFrameworkElement);
65: Canvas.SetLeft(objFrameworkElement, mousePos1.X - mousePos2.X);
66: Canvas.SetTop(objFrameworkElement, mousePos1.Y - mousePos2.Y);
67: }
68: break;
69: }
70: }
71: }
72: }
73: #endregion