I have placed text blocks on a canvas. I change their position on mouse drag using the following code:
protected bool isDragging;
private Point clickPosition;
private void txt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isDragging = true;
var draggableControl = sender as TextBlock;
clickPosition = e.GetPosition(this);
draggableControl.CaptureMouse();
}
private void txt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isDragging = false;
var draggable = sender as TextBlock;
draggable.ReleaseMouseCapture();
}
private void txt_MouseMove(object sender, MouseEventArgs e)
{
var draggableControl = sender as TextBlock;
if (isDragging && draggableControl != null)
{
Point currentPosition = e.GetPosition(this.Parent as UIElement);
var transform = draggableControl.RenderTransform as TranslateTransform;
if (transform == null)
{
transform = new TranslateTransform();
draggableControl.RenderTransform = transform;
}
transform.X = currentPosition.X - clickPosition.X;
transform.Y = currentPosition.Y - clickPosition.Y;
}
}
What I want here is to get the new position of the textblock, save it and next time when the application loads, the control should take the new position. I understand that my code does not change the Left & Top property of the textblock, but instead transforms it. Hence, the Left and Top Property won't be changed. I tried to set Left and Top Property as
draggableControl.SetValue(Canvas.LeftProperty, transform.X);
draggableControl.SetValue(Canvas.TopProperty, transform.Y);
But it also does not change it.
What can be done to achieve the desired?
You must not use Transform...but only
draggableControl.SetValue(Canvas.LeftProperty, X);
draggableControl.SetValue(Canvas.TopProperty, Y);
because transform affect only rendering
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.