-1

我在其中创建了一个由 a和 aShape构造的自定义。这是代码:RectangleText

    protected override Geometry DefiningGeometry
    {
        get
        {
            var formattedText = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Transparent);
            var chosenTextPoint = new Point
            {
                X = ((Location.X < BottomRight.X) ? Location.X : BottomRight.X) + 5,
                Y = ((Location.Y < BottomRight.Y) ? Location.Y : BottomRight.Y) + 5
            };

            Stroke = Brushes.ForestGreen;
            StrokeThickness = (IsSelected) ? HighlightedValue : HighlightedValue / 2;

            Rectangle = new Rect(Location, BottomRight);

            var rectangleGeometry = new RectangleGeometry(Rectangle);
            var textGeometry = formattedText.BuildGeometry(chosenTextPoint);

            var combinedGeometry = new CombinedGeometry
            {
                GeometryCombineMode = GeometryCombineMode.Xor,
                Geometry1 = rectangleGeometry,
                Geometry2 = textGeometry
            };

            combinedGeometry.Geometry1.SetValue(FillProperty, Brushes.Blue);
            combinedGeometry.Geometry1.InvalidateProperty(FillProperty);
            Fill = (IsSelected) ? Brushes.Transparent : null;

            return combinedGeometry;
        }
    }

combinedGeometry是我最近添加的,在此之前我使用PathGeometry. 在这两种情况下, theRectangle和 theText都以相同的颜色着色,并且“遭受”相同的形状效果。

我有办法将两者分开吗?通过分离我的意思是它们都是内部的单个元素Shape,我可以自由地修改它们中的任何一个或一起修改它们?

4

1 回答 1

0

AGeometry没有颜色 - 这正是它的名字所暗示的。Shapes 使用填充和描边画笔绘制单个几何体。

您可以:

  • 使用多种形状。这可能会很昂贵,因为每个Shape都是可以接受输入、渲染等的成熟控件。
  • 使用 a Drawing,它可以包含带有多个画笔的多个几何图形,然后使用控件中的(DrawingImage一种ImageSourceImageDrawingBrush(一种Brush)作为其他一些控件(例如 a Rectangle)的填充/背景来渲染它。请注意,Blend(Visual Studio 附带的工具)可以将一组控件转换为DrawingBrush(工具 > 制作画笔)。
  • 从头开始创建一个控件,继承自FrameworkElement并通过覆盖自己呈现它OnRender
于 2016-07-19T19:18:58.410 回答