WPF在另一个形状内绘制一个形状

时间:2018-07-17 13:51:51

标签: c# wpf xaml canvas shape

我必须在空气动力学方面构建具有不同矩形的画布,并且其中一些必须交叉。我通过添加2条线作为矩形的对角线来实现。 问题是当我将线设置为较粗时,线将越过矩形轮廓,如图所示: enter image description here

是否可以将线设置为仅在矩形内?

这是我用来添加矩形和线条的代码:

private void DrawRectangle()
    {
        var rectangle = new Rectangle();
        rectangle.Height = 100;
        rectangle.Width = 100;
        rectangle.Fill = Brushes.Yellow;
        rectangle.Stroke = System.Windows.Media.Brushes.Blue;
        rectangle.StrokeThickness = 1;

        _canvas.Children.Add(rectangle);
    }

    private void DrawBroken(Rectangle rectangle, long left, long bottom)
    {
        DrawBrokenLine(0, 0, 100, 100);
        DrawBrokenLine(0, 100, 100, 0);
    }

    private void DrawBrokenLine(long x1, long y1, long x2, long y2)
    {
        var line = new Line();
        line.X1 = x1;
        line.Y1 = y1;
        line.X2 = x2;
        line.Y2 = y2;
        line.Stroke = Brushes.Indigo;
        line.StrokeThickness = 10;

        _canvas.Children.Add(line);
    }

3 个答案:

答案 0 :(得分:0)

您可以将Rectangle和Lines添加到另一个裁剪其子元素的Canvas中:

<Canvas Width="100" Height="100" ClipToBounds="True">
   <Rectangle Width="100" Height="100" Fill="Yellow" Stroke="Blue" StrokeThickness="1"/>
   <Line X1="0" Y1="0" X2="100" Y2="100" Stroke="Indigo" StrokeThickness="10"/>
   <Line X1="0" Y1="100" X2="100" Y2="0" Stroke="Indigo" StrokeThickness="10"/>
</Canvas>

答案 1 :(得分:0)

您可以使用DrawingBrush并将Shape Background属性设置为此。

<Rectangle Width="100" Height="100" Stroke="Black" StrokeThickness="1">
  <Rectangle.Fill>
    <DrawingBrush >
      <DrawingBrush.Drawing>                
        <GeometryDrawing Geometry="M0,0 L1,1 M1,0 L0,1"
          Stroke="Purple" StrokeThickness="10"/>                    
       </DrawingBrush.Drawing>
     </DrawingBrush>
   </Rectangle.Fill>
 </Rectangle>

更新:

this page中有一个示例,演示了如何以编程方式使用DrawingBrush:

// Create a DrawingBrush.
DrawingBrush myDrawingBrush = new DrawingBrush();

// Create a drawing.
GeometryDrawing myGeometryDrawing = new GeometryDrawing();
myGeometryDrawing.Brush = Brushes.LightBlue;
myGeometryDrawing.Pen = new Pen(Brushes.Gray, 1);
GeometryGroup ellipses = new GeometryGroup();
ellipses.Children.Add(new EllipseGeometry(new Point(25,50), 12.5, 25));
ellipses.Children.Add(new EllipseGeometry(new Point(50,50), 12.5, 25));
ellipses.Children.Add(new EllipseGeometry(new Point(75,50), 12.5, 25));

myGeometryDrawing.Geometry = ellipses;
myDrawingBrush.Drawing = myGeometryDrawing;

Button myButton = new Button();
myButton.Content = "A Button";

// Use the DrawingBrush to paint the button's background.
myButton.Background = myDrawingBrush;

您不需要做相同的事情,但是您的想法是创建一个Drawing(所需的两条对角线,可以是GeometryDrawing),然后从中创建一个DrawingBrush,您可以在其中使用您的矩形。

答案 2 :(得分:0)

如果您不喜欢线条的平方端,可以设置其端盖。

<Line X1="0" Y1="0" X2="100" Y2="100" Stroke="Indigo" StrokeThickness="10" StrokeEndLineCap="Triangle" StrokeStartLineCap="Triangle"/>
<Line X1="0" Y1="100" X2="100" Y2="0" Stroke="Indigo" StrokeThickness="10" StrokeEndLineCap="Triangle" StrokeStartLineCap="Triangle"/>

此外,请记住将StrokeThickness设置为10,将绘制直线的起点和终点,因此您可能需要相应地进行调整(除非像建议的其他人一样进行裁剪)。在开头加上4(0 => 4),然后从结尾减去4(100 => 96)。