【问题标题】:Defining style for GeometryDrawing为 GeometryDrawing 定义样式
【发布时间】:2017-08-10 05:49:23
【问题描述】:

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

我需要为大约 180 多个 GeometryDrawing 对象设置 PenBrush 属性。我的第一个想法是使用Style,但我了解到Style 不能以GeometryDrawing 为目标,因为它不是从FrameworkElement 继承的。然后我考虑创建自己的GeometryDrawing2,它继承自GeometryDrawing,并在构造函数中设置PenBrush,但发现GeometryDrawing是密封的,不能被继承。

除了复制粘贴属性 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>

我需要为每个GeometryDrawing 对象设置BrushPen 来表示RedBlack。 XAML 中通常的干净方式是通过Style 执行此操作,但正如我在上面解释的那样,这不适用于GeometryDrawing。唯一的另一种方法是将Brush="{StaticResource MyBrush}"Pen="{StaticResource MyPen}" 复制粘贴到180 多个GeometryDrawing 对象中的每一个。还是有更快(仅限 XAML)的方式?

【问题讨论】:

  • 不清楚你的意思。你在哪里复制/粘贴属性?您如何存储 GeometryDrawing 对象?为什么它们不在您可以枚举并以这种方式分配值的集合中?请改进您的问题,以便清楚您现在正在做什么以及您希望能够做什么,并确保包含一个很好的 minimal reproducible example 来说明所有这些。
  • @PeterDuniho:查看我的编辑。
  • @Funk 根本没有重复。 OP 没有样式。
  • @Clemens 你看标题了吗?

标签: c# wpf xaml wpf-style geometrydrawing


【解决方案1】:

您的问题仍然不完整,缺少一个好的Minimal, Complete, and Verifiable code example 可以使用。但是,根据您迄今为止提供的信息,我希望基于模板的实现可以解决您的问题。例如:

<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>

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

<ContentControl Content="{StaticResource geometry1}"/>

【讨论】:

  • 很公平。这显然解决了手头的问题。非常感谢。
猜你喜欢
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多