我试图在 Wpf 中的 Canvas
上移动(按住鼠标左键并拖动)UIElement
。
它对 Rectangle
有效,但是当我对 Path
形状尝试相同时它不会移动。
这是布局, Canvas 内只有 2 个元素:
<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>
这是代码隐藏:
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);
}
}
}
Path
是一个类似于 Rectangle
的 Shape
并且为两个控件执行 OnMouseMove
。
为什么只有 Rectangle
在移动而 Path
没有?
最佳答案
改用 ActualWidth 和 ActualHeight。如果检查,宽度和高度为 NaN。
关于c# - 为什么我不能在 WPF 中移动路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58310861/