How to drag a UserControl inside a Canvas

后端 未结 7 1242
Happy的楠姐
Happy的楠姐 2020-11-28 07:28

I have a Canvas in which user can add UserControl subclasses containing a form. User should be able to drag these UserControl around the Canvas.

What\'s the best pr

7条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 08:02

    Regarding Corey Sunwold solution - I got rid of MouseUp and MouseDown events and I simplified MouseMove method using MouseButtonState as below :) I'm using Canvas.SetLeft() and Canvas.SetTop() instead RenderTransform so I don't need to store old position from MouseDown event.

    if (e.LeftButton == MouseButtonState.Pressed && draggableControl != null)
    {
       //...
    }
    

提交回复
热议问题