如何在两个椭圆之间画线?

时间:2019-07-17 09:09:28

标签: wpf

很难概括我的问题。假设我动态地创建了两个椭圆。现在,我想在它们之间画一条线。我无法使用椭圆的边距来定位线条的Y轴,因为我正在使用网格行垂直放置我的椭圆。有人可以帮忙吗?

Path path = new Path();
path.Fill = new SolidColorBrush(Colors.Green);
path.StrokeThickness = 2;
Binding binding = new Binding("center") { ElementName = "ellipse" };
Binding binding_to = new Binding("center") { ElementName = "ellipse1" };
LineGeometry lineGeometry = new LineGeometry();
BindingOperations.SetBinding(lineGeometry, LineGeometry.StartPointProperty, binding);
BindingOperations.SetBinding(lineGeometry, LineGeometry.EndPointProperty, binding_to);
path.Data = lineGeometry;
map_grid.Children.Add(path);

1 个答案:

答案 0 :(得分:0)

根本不使用网格。最好使用两个具有适当EllipseGeometries的Path元素,并将EllipseGeometries的Center点用作第三个Path元素中LineGeometry的端点。将它们全部放在一个普通的画布中:

<Canvas>
    <Path Stroke="Black" StrokeThickness="3">
        <Path.Data>
            <LineGeometry StartPoint="{Binding ElementName=e1, Path=Center}"
                          EndPoint="{Binding ElementName=e2, Path=Center}"/>
        </Path.Data>
    </Path>

    <Path Fill="Red">
        <Path.Data>
            <EllipseGeometry x:Name="e1" Center="100,100" RadiusX="50" RadiusY="50"/>
        </Path.Data>
    </Path>

    <Path Fill="Green">
        <Path.Data>
            <EllipseGeometry x:Name="e2" Center="200,300" RadiusX="30" RadiusY="30"/>
        </Path.Data>
    </Path>
</Canvas>