为什么我不能在 WPF 中移动路径?
Why I can't move a Path in WPF?
我试图在 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。
我试图在 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。