2

请不要将其标记为重复。链接的问题与我所问的完全不同。

我需要为大约 180 多个对象设置Pen和属性。我的第一个想法是为它使用 a ,但我了解到无法定位,因为它不继承自. 然后我考虑在构造函数中创建自己的继承和设置,但发现它是密封的并且不能被继承。BrushGeometryDrawingStyleStyleGeometryDrawingFrameworkElementGeometryDrawing2GeometryDrawingPenBrushGeometryDrawing

除了复制粘贴属性 180 次之外,实现我想要的最佳方法是什么?

编辑

这是我收集的图纸:

<ResourceDictionary>
  <DrawingImage x:Key="Drawing1">
    <DrawingImage.Drawing>
      <GeometryDrawing Geometry="..." />
    </DrawingImage.Drawing>
  </DrawingImage>

  <DrawingImage x:Key="Drawing2">
    <DrawingImage.Drawing>
      <GeometryDrawing Geometry="..." />
    </DrawingImage.Drawing>
  </DrawingImage>

  <!--180 more such drawings-->
</ResourceDictionary>

我需要为每个对象设置Brushand来表示and 。XAML中通常的干净方式是通过. 唯一的另一种方法是复制粘贴到180 多个对象中的每一个。还是有更快(仅限 XAML)的方式?PenGeometryDrawingRedBlackStyleGeometryDrawingBrush="{StaticResource MyBrush}"Pen="{StaticResource MyPen}"GeometryDrawing

4

1 回答 1

1

您的问题仍然不完整,缺少一个好的最小、完整和可验证的代码示例。但是,根据您迄今为止提供的信息,我希望基于模板的实现可以解决您的问题。例如:

<ResourceDictionary>
  <PathGeometry x:Key="geometry1">...</PathGeometry>
  <PathGeometry x:Key="geometry2">...</PathGeometry>
  <!-- etc. -->

  <DataTemplate DataType="{x:Type PathGeometry}">
    <DrawingImage>
      <DrawingImage.Drawing>
        <GeometryDrawing Geometry="{Binding}" Pen="..." Brush="..."/>
      </DrawingImage.Drawing>
    </DrawingImage>
  </DataTemplate>
</ResourceDictionary>

然后,当您想显示其中一个时,只需使用 aContentControlContentPresenter。例如:

<ContentControl Content="{StaticResource geometry1}"/>
于 2017-08-10T08:01:01.843 回答