我想绘制一个简单的 Path
,它使用 Polygon
的 RenderedGeometry
作为 Data
。
Polygon polygon = new Polygon();
polygon.Points = new PointCollection { new Point(0, 0), new Point(0, 100), new Point(150, 150) };
var path = new Path
{
Data = polygon.RenderedGeometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Panel.SetZIndex(path, 2);
canvas.Children.Add(path);
但是我的 Canvas
没有显示任何内容。
最佳答案
您应该在将几何体渲染到 Canvas
之前强制渲染它。您可以通过调用 Polygon
的 Arrange
和 Measure
方法来执行此操作:
Polygon polygon = new Polygon();
polygon.Points = new PointCollection { new Point(0, 0), new Point(0, 100), new Point(150, 150) };
polygon.Arrange(new Rect(canvas.RenderSize));
polygon.Measure(canvas.RenderSize);
var path = new Path
{
Data = polygon.RenderedGeometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Panel.SetZIndex(path, 2);
canvas.Children.Add(path);
关于c# - 使用 RenderedGeometry 作为路径数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43606765/