Your flicker is caused by a conversion in the PreviewMouseMove handler.
You transform the X and Y positions of the control using the mouse position relative to your Button control, which will change each time your transform is applied. Each time you change the position of the button, your Mouse.GetPosition(sender as Button).X and Y return different values, causing the button to change position again, etc.
One way: get the mouse position from the parent (for example, Grid , Canvas with a fixed position):
<Grid Name="myGrid"> <Button Name="Samplebutton" PreviewMouseDown="Samplebutton_PreviewMouseDown" PreviewMouseUp="Samplebutton_PreviewMouseUp" PreviewMouseMove="Samplebutton_PreviewMouseMove" Content="Moving" Width="100" Height="35"/> </Grid>
and
private void Samplebutton_PreviewMouseMove(object sender, MouseEventArgs e) { if (m_IsPressed) { TranslateTransform transform = new TranslateTransform(); transform.X = Mouse.GetPosition(myGrid).X; transform.Y = Mouse.GetPosition(myGrid).Y; this.Samplebutton.RenderTransform = transform; } }
This is not the most beautiful thing in the world, as it will βgrabβ the button in the upper left corner, but you can adapt the position to get the desired behavior.
Good luck.
Edit: If you do not want to explicitly specify the element that you want to use for the relative position, you should be able to select the immediate parent by querying the sender object:
transform.X = Mouse.GetPosition((sender as Button).Parent as FrameworkElement).X;
Chris source share