我正在尝试用wpf创建自定义形状。首先,我只是试图创建一个简单的线,在每一端都有一个圆(我知道有线帽,但这不是我要找的)。
我查阅了一些教程,最简单的方法似乎是使用CombinedGeometry。但是我不能让它正常工作。下面是创建几何体对象的代码:

protected override Geometry DefiningGeometry
    {
        get
        {
            Point ellipseCenter1 = new Point(X1 - this.CapDiameter / 2, Y1 - this.CapDiameter / 2);
            Point ellipseCenter2 = new Point(X2 - this.CapDiameter / 2, Y2 - this.CapDiameter / 2);
            var ellipse1 = new EllipseGeometry(ellipseCenter1, CapDiameter, CapDiameter);
            var ellipse2 = new EllipseGeometry(ellipseCenter2, CapDiameter, CapDiameter);
            var line = new LineGeometry(this.StartPoint, this.EndPoint);

            var combined1 = new CombinedGeometry(GeometryCombineMode.Union, ellipse1, line);
            var combined2 = new CombinedGeometry(GeometryCombineMode.Union, combined1, ellipse2);

            // Freeze the geometry for performance benefits
            combined2.Freeze();

            return combined2;
        }
    }

然而,出于某种原因,这并没有划清界限。它画两个圆,但不画线。很明显我遗漏了一些东西,有人能指出什么吗?:)
为了以防万一,下面是全班的其他同学:
#region Dependency Properties
public static readonly DependencyProperty X1Property =
    DependencyProperty.Register(
        "X1",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty Y1Property =
    DependencyProperty.Register(
        "Y1",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty X2Property =
    DependencyProperty.Register(
        "X2",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty Y2Property =
    DependencyProperty.Register(
        "Y2",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty CapDiameterProperty =
    DependencyProperty.Register(
        "CapDiameter",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );

#endregion
#region CLR Properties

public double X1
{
    get { return (double)base.GetValue(X1Property); }
    set { base.SetValue(X1Property, value); }
}

public double Y1
{
    get { return (double)base.GetValue(Y1Property); }
    set { base.SetValue(Y1Property, value); }
}

public double X2
{
    get { return (double)base.GetValue(X2Property); }
    set { base.SetValue(X2Property, value); }
}

public double Y2
{
    get { return (double)base.GetValue(Y2Property); }
    set { base.SetValue(Y2Property, value); }
}

public Point StartPoint
{
    get { return (new Point(X1, Y1)); }
}

public Point EndPoint
{
    get { return (new Point(X2, Y2)); }
}

public double CapDiameter
{
    get { return (double)base.GetValue(CapDiameterProperty); }
    set { base.SetValue(CapDiameterProperty, value); }
}

#endregion

下面是我用来测试它的XAML代码:
<Window x:Class="HG.WPF.Shapes.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:shapes="clr-namespace:Tomers.WPF.Shapes"

    Title="WPF Custom Shapes" Height="300" Width="300" ResizeMode="NoResize">

    <Canvas Background="Black">
        <shapes:CappedLine X1="30" Y1="30" X2="200" Y2="200" CapDiameter="5" Fill="White" Stroke="White" StrokeThickness="2"></shapes:CappedLine>
    </Canvas>

</Window>

最佳答案

如果您尝试LineGeometry对象,则它们将消失。来自MSDN:
geometrycombinemode属性指定两个几何图形将如何
合并。注意combinedGeometry结合了
两个几何图形,因此没有面积的几何图形(例如
线几何)合并时消失。
您可以使用Union

GeometryGroup combined = new GeometryGroup();
combined.Children.Add(ellipse1);
combined.Children.Add(ellipse2);
combined.Children.Add(line);

或者,可以先对椭圆对象使用GeometryGroup,然后使用CombinedGeometry将其与直线分组。

09-25 21:39