c# - 使用 RenderedGeometry 作为路径数据不起作用

标签 c# wpf path polygon

我想绘制一个简单的 Path,它使用 PolygonRenderedGeometry 作为 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 之前强制渲染它。您可以通过调用 PolygonArrangeMeasure 方法来执行此操作:

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/

相关文章:

macos - 从 OS X 命令行调用的 ctags 不正确

c++ - Qt:QString 中的文件路径

c# - 使用 httpClient 获取 JSON 字符串

c# - 将 Action 注入(inject) UserControl

wpf - VisualStateManager VisualGroup 优先级

c# - 使用 WPF 窗体 DataGrid、DataTable 和 Combobox

python - 如何在 VIM 中设置文件的正确路径?

C# DataGridView 右键单击​​到 ContextMenu 单击检索单元格值

c# - 使用参数运行 ASP.NET 页面?

C#:特定于范围的变量绑定(bind)