When I drop a TextBlock onto a Canvas I use the following code:
var canvas = e.DropTarget as Canvas; var draggedElement = e.OriginalDragSource; FrameworkElement newElement = null; if (draggedElement is TextBlock) { var oldText = draggedElement as TextBlock; newElement = new TextBlock { Text = oldText.Text, TextWrapping = TextWrapping.Wrap, Height = oldText.ActualHeight, Width = oldText.ActualWidth }; } canvas.Children.Add(newElement); Point dropPoint = e.GetPosition(canvas); // Need to get point relative to text as well newElement.SetValue(Canvas.TopProperty, dropPoint.Y); newElement.SetValue(Canvas.LeftProperty, dropPoint.X);
However, the TextBlock is offset as the drop point is the location of the cursor which may be anywhere along the selected text:
Text Being Dragged
^
The same point is returned whether I pass the Canvas or the TextBlock into e.GetPosition.
Hi Chris,
You can handle the TextBlock’s MouseLeftButtonDown event to get the handle point like
Point handlePoint;
private void TextBlock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
handlePoint = e.GetPosition(sender as TextBlock);
}
Next in your Drop Event you can calculate the position like
Point dropPoint = e.GetPosition(canvas);
// Need to get point relative to text as well
newElement.SetValue(Canvas.TopProperty, dropPoint.Y - handlePoint.Y);
newElement.SetValue(Canvas.LeftProperty, dropPoint.X - handlePoint.X);
Best,
Ivo
This was the solution I came up with myself, but I was hoping for something a little more .. elegant - something that was part of the drag/drop process itself.
However, if that's all we have....
Chris, you are right.
I've just filed a Product Idea to have this Point in DropEventArgs.
Hello,
I am glad that you have found a solution to your issue.
Thank you for using Infragistics Components.
I'm using the work around already suggested (which I came up with independently) so until the product idea is implemented this issue is closed.
I am just checking your progress on the issue. If you require any further assistance please do not hesitate to ask.