C# 在WPF装饰器中绘制虚线

C# 在WPF装饰器中绘制虚线,c#,.net,wpf,xaml,graphics,C#,.net,Wpf,Xaml,Graphics,我在网上找到了几篇关于在WPF中绘制虚线的文章。然而,它们似乎围绕使用Line类展开,Line类是WPF中的UIElement。事情是这样的: Line myLine = new Line(); DoubleCollection dashes = new DoubleCollection(); dashes.Add(2); dashes.Add(2); myLine.StrokeDashArray = dashes; 现在,我在一个装饰器中,在那里我只能访问绘图上下文。在那里,我或多或少地被简

我在网上找到了几篇关于在WPF中绘制虚线的文章。然而,它们似乎围绕使用Line类展开,Line类是WPF中的UIElement。事情是这样的:

Line myLine = new Line();
DoubleCollection dashes = new DoubleCollection();
dashes.Add(2);
dashes.Add(2);
myLine.StrokeDashArray = dashes;
现在,我在一个装饰器中,在那里我只能访问绘图上下文。在那里,我或多或少地被简化为绘图原语、画笔、钢笔、几何体等。这看起来更像:

var pen = new Pen(new SolidColorBrush(Color.FromRgb(200, 10, 20)), 2);
drawingContext.DrawLine(pen, point1, point2);
我被困在如何在这个API级别上绘制虚线的问题上。我希望这不是“一条一条地画小线”,而是我没有看到的其他东西…

看看这个酒店。您可以使用提供某些预定义的短划线样式的类成员,也可以通过创建新实例来指定自己的短划线和间距模式

var pen = new Pen(new SolidColorBrush(Color.FromRgb(200, 10, 20)), 2);
pen.DashStyle = DashStyles.Dash;
drawingContext.DrawLine(pen, point1, point2);

您不必拘泥于基本体。如果遵循此模式,则可以向装饰器添加任何内容

public class ContainerAdorner : Adorner
{
    // To store and manage the adorner's visual children.
    VisualCollection visualChildren;

    // Override the VisualChildrenCount and GetVisualChild properties to interface with 
    // the adorner's visual collection.
    protected override int VisualChildrenCount { get { return visualChildren.Count; } }
    protected override Visual GetVisualChild(int index) { return visualChildren[index]; }

    // Initialize the ResizingAdorner.
    public ContainerAdorner (UIElement adornedElement)
        : base(adornedElement)
    {
        visualChildren = new VisualCollection(this);
        visualChildren.Add(_Container);
    }
    ContainerClass _Container= new ContainerClass();

    protected override Size ArrangeOverride(Size finalSize)
    {
        // desiredWidth and desiredHeight are the width and height of the element that's being adorned.  
        // These will be used to place the Adorner at the corners of the adorned element.  
        double desiredWidth = AdornedElement.DesiredSize.Width;
        double desiredHeight = AdornedElement.DesiredSize.Height;

        FrameworkElement fe;
        if ((fe = AdornedElement as FrameworkElement) != null)
        {
            desiredWidth = fe.ActualWidth;
            desiredHeight = fe.ActualHeight;
        }

        _Container.Arrange(new Rect(0, 0, desiredWidth, desiredHeight));

        return finalSize;
    }
}

Doh,就是这样,不知怎的我错过了那家酒店。目前德国气温为35摄氏度以上:)