c# - 为什么我不能在 WPF 中移动路径?

标签 c# wpf path shapes

我试图在 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 是一个类似于 RectangleShape 并且为两个控件执行 OnMouseMove

为什么只有 Rectangle 在移动而 Path 没有?

最佳答案

改用 ActualWidth 和 ActualHeight。如果检查,宽度和高度为 NaN。

关于c# - 为什么我不能在 WPF 中移动路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58310861/

相关文章:

c# - map 初始化后将标记添加到谷歌地图

c# - WPF通过转换器绑定(bind)数据网格中的图像列,无法在属性更改时更新图像

linux - 这是一个有效的 Go 路径配置吗?

shell - 将目录添加到 PATH 是什么意思?

c# - MySQL .Net 连接池 connection.Open() 很慢

c# - C# 中带有 char 键的不区分大小写的字典

c# - 如何从最近的点找到路径上的点?

powershell - 使用-Path时包含[和(

c# - 如何计算两点之间的值百分比值

c# - 在客户端加密 ASP.NET 文本字段并在服务器端解密