Hi I have strange problem with drag and drop operation between two canvases. I wanted to drag my custom control from one canvas to another.
I started dragging operation with this code
DataObject dragObject = new DataObject("CalendarBlock",block);
DragDrop.DoDragDrop(this, dragObject, DragDropEffects.Move);
In second canvas I wrote this code
void CalendarCanvas_PreviewDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("CalendarBlock"))
{
UcCalendarBlock calendarBlock = e.Data.GetData("CalendarBlock") as UcCalendarBlock;
CalendarCanvas parent = calendarBlock.Parent as CalendarCanvas;
// parent.Children.Remove(calendarBlock);
(e.Source as CalendarCanvas).Children.Remove(calendarBlock);
this.AddCalendarTask(calendarBlock);
}
}
The problem is that I get an error
"Specified element is already the logical child of another element. Disconnect it first."
It was strange because I already removed this element using that code
(e.Source as CalendarCanvas).Children.Remove(calendarBlock);
However it truned out that (e.Source as CalendarCanvas).Children collection is empty(I have no idea why), so I used that code
parent.Children.Remove(calendarBlock);
Now everything is working fine, however I think is not too elegant solution. Could somebody tells me why (e.Source as CalendarCanvas).Children is empty ??