#region MouseLeftButtonUp
void objFrameworkElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//Stop Drag
FrameworkElement objFrameworkElement = (FrameworkElement)sender;
objFrameworkElement.ReleaseMouseCapture();
objFrameworkElement.MouseMove -=
new MouseEventHandler(objFrameworkElement_MouseMove);
objFrameworkElement.MouseLeftButtonUp -=
new MouseButtonEventHandler(objFrameworkElement_MouseLeftButtonUp);
// If it is an element marked [draggable]
// try to drop it on a panel stored in the colPanels collection
if (objFrameworkElement.Tag.ToString().Contains("[draggable]"))
{
Point tmpPoint = e.GetPosition(null);
// Build a list of elements at the current mouse position
List<UIElement> hits = VisualTreeHelper.FindElementsInHostCoordinates( tmpPoint, this ) as List<UIElement>;
// Loop through all the Panels in the colPanels collection
foreach(Panel objPanel in colPanels)
{
if (hits.Contains(objPanel))
{
// Grab the position of the element being dragged in relation to it's position on the
// main canvas and its position in relation to the panel it may be dropped on
Point mousePos1 = e.GetPosition(objPanel);
Point mousePos2 = e.GetPosition(objFrameworkElement);
// Remove the element from the main canvas
this.LayoutRoot.Children.Remove(objFrameworkElement);
// Import content
// Get a reference to the parent of the current panel
UserControl objUserControl = (UserControl)objPanel.Parent;
// See if that parent implements an interface called "ImportContent"
object objObject = objUserControl.GetType().GetInterface("ImportContent", true);
// If the object is not null then the parent object has a method called "ImportContent"
if (!(objObject == null))
{
// Create a parmeters array
object[] parameters = new object[1];
// Add the elemnt that is being dragged to the array
parameters.SetValue(objFrameworkElement, 0);
// invoke the "ImportContent" on the parent object passing the parameters array that
// contains the element being dragged
bool boolImport = (bool)objUserControl.GetType().InvokeMember("ImportContent",
BindingFlags.InvokeMethod, null, objUserControl, parameters);
// If the import was not successful simply add the element to the panel
if (!boolImport)
{
// Add the element to the panel
objPanel.Children.Add(objFrameworkElement);
Canvas.SetLeft(objFrameworkElement, mousePos1.X - mousePos2.X);
Canvas.SetTop(objFrameworkElement, mousePos1.Y - mousePos2.Y);
}
}
else
{
// The parent object does not implement the "ImportContent" Interface
// Add the element to the panel
objPanel.Children.Add(objFrameworkElement);
Canvas.SetLeft(objFrameworkElement, mousePos1.X - mousePos2.X);
Canvas.SetTop(objFrameworkElement, mousePos1.Y - mousePos2.Y);
}
break;
}
}
}
}
#endregion