I tried to move (hold the left mouse button and drag) a UIElement
on a Canvas
in Wpf.
It worked for a Rectangle
but when I tried the same to a Path
shape it does not move.
Here is the layout, just 2 elements inside a canvas:
<Canvas Background='Beige'
Name='canvas'>
<Rectangle Width='50'
Height='50'
Fill='LightPink'
Canvas.Left='350'
Canvas.Top='175'
MouseMove='OnMouseMove'
Name='square' />
<Path Fill="Cyan"
Stroke="Black"
MouseMove='OnMouseMove'>
<Path.Data>
<GeometryGroup>
<EllipseGeometry Center="20, 40"
RadiusX="20"
RadiusY="40" />
<EllipseGeometry Center="20, 40"
RadiusX="10"
RadiusY="30" />
</GeometryGroup>
</Path.Data>
</Path>
</Canvas>
And here is the code-behind:
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (e.Source is Shape shape)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point p = e.GetPosition(canvas);
Canvas.SetLeft(shape, p.X - shape.Width / 2);
Canvas.SetTop(shape, p.Y - shape.Height / 2);
}
}
}
The Path
is a Shape
like the Rectangle
and OnMouseMove
is executed for both controls.
Why only the Rectangle
is moving and the Path
isn't?