【问题标题】:Adding (BezierSegment to a) Path to Canvas将(BezierSegment 到 a)路径添加到画布
【发布时间】:2013-07-20 20:31:49
【问题描述】:

我目前正在尝试为我的 WPF 添加一个BezierSegment 到我的画布上。我收到了一个不正确的转换的编译时错误:

参数 1:无法从“System.Windows.Media.PathGeometry”转换为“System.Windows.UIElement”

这就是我目前所拥有的......

//bezier curve it 
BezierSegment curve = new BezierSegment(startPoint, endPoint,controlPoint,false);

// Set up the Path to insert the segments
PathGeometry path = new PathGeometry();

PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = hs.LeStartingPoint;
pathFigure.IsClosed = true;
path.Figures.Add(pathFigure);

pathFigure.Segments.Add(curve);
System.Windows.Shapes.Path p = new Path();
p.Data = path;
this.mainWindow.MyCanvas.Children.Add(path);

任何帮助将不胜感激!

【问题讨论】:

    标签: c# wpf drawing bezier


    【解决方案1】:

    您必须将p (Path) 添加到Canvas,而不是path (PathGeometry)。

    BezierSegment curve = new BezierSegment(new Point(11,11), new Point(22,22), new Point(15,15), false);           
    
    // Set up the Path to insert the segments
    PathGeometry path = new PathGeometry();
    
    PathFigure pathFigure = new PathFigure();
    pathFigure.StartPoint = new Point(11, 11);
    pathFigure.IsClosed = true;
    path.Figures.Add(pathFigure);
    
    pathFigure.Segments.Add(curve);
    System.Windows.Shapes.Path p = new Path();
    p.Stroke = Brushes.Red;
    p.Data = path;
    
    MyCanvas.Children.Add(p); // Here
    

    【讨论】:

      【解决方案2】:

      我现在不在可以检查它的机器附近,但我想你快到了。您需要添加您创建的 System.Windows.Shapes.Path 对象(您当前没有使用它),以及一些参数以使线条实际呈现:

       System.Windows.Shapes.Path p = new Path();
       p.Data = path;
       p.Fill = System.Windows.Media.Brushes.Green;
       p.Stroke = System.Windows.Media.Brushes.Blue;
       p.StrokeThickness = 1;
      
       this.MyCanvas.Children.Add(p);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-16
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        相关资源
        最近更新 更多