【问题标题】:Is it possible to alter the bounds of a WPF element?是否可以更改 WPF 元素的边界?
【发布时间】:2020-01-27 03:26:43
【问题描述】:

我正在动态创建一个Path,我注意到形状的边界Rect 远大于路径本身,如下图所示。

是否可以改变这些界限?不幸的是,PathGeometry.Bounds 属性是只读的。

ps:如果有帮助,我有兴趣缩小界限,因此我可以将路径的 RenderTransformOrigin 设置为 new Point(0.5,0.5) 以围绕自身旋转 (RotateTransform)。

PathFigure的创建

我定义了 4 个点,然后创建了三个 LineSegments 和 ArcSegment。我创建了一个 Circle 结构体,用作计算这些点的指南。

private PathFigure CreateFigure()
        {
            var lineAB = new LineSegment(pointB, true);
            var arcBC = new ArcSegment(pointC, new Size(_outerCircle.Radius, _outerCircle.Radius), 0, false, SweepDirection.Clockwise, true);
            var lineCD = new LineSegment(pointD, true);

            return new PathFigure(pointA, new List<PathSegment> { lineAB, arcBC, lineCD }, true);
        }

【问题讨论】:

  • 如何创建路径?除非你做了一些奇怪的事情,否则界限是没有意义的。除此之外,你只想旋转那个圆段?
  • 但是这条路是我想要的。界限就是问题。老实说PathGeometry.Transform 看起来很有趣。我认为只能为Visuals 设置转换。
  • 您应该确保使用以 (0,0) 为中心的坐标,如我的回答中所示,圆心位于 (0,0)。

标签: wpf bounding-box bounds


【解决方案1】:

由于在定义边界后无法更改边界,因此将 Path 作为子级添加到 GridBorder 并设置 Path.Stretch="Fill" 将强制边界由路径填充布局传递。

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 2013-04-01
    • 2019-09-26
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    相关资源
    最近更新 更多