WPF 复合形状

发布于 2024-09-04 09:11:29 字数 103 浏览 1 评论 0原文

我使用基本形状创建了一个有点复杂的形状。我想在画布上使用多个这个,在代码隐藏中以编程方式创建。

我考虑过创建一个 UserControl,但是封装这个复合形状的最佳方法是什么?

I have created a somewhat complex shape using basic shapes. I would like to use multiples of this on a canvas, created programmatically in code behind.

I have thought about creating a UserControl, but what's the best way to encapsulate this composite shape?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-09-11 09:11:29

对于大多数目的,将它们放入 ControlTemplate 或 DataTemplate 效果最佳。以下是 ControlTemplate 方式:

<ResourceDictionary>
  <ControlTemplate x:Key="MyShape">
    <Grid With="..." Height="...">
      <Rectangle ... />
      <Ellipse ... />
      <Path ... />
    </Grid>
  </ControlTemplate>
</ResourceDictionary>

...
<Canvas ...>
  <Control Template="{StaticResource MyShape}" ... />
  <Control Template="{StaticResource MyShape}" ... />
  <Control Template="{StaticResource MyShape}" ... />
  <Control Template="{StaticResource MyShape}" ... />
</Canvas>

和 DataTemplate 方式:

<ResourceDictionary>
  <DataTemplate x:Key="MyShape">
    <Grid With="..." Height="...">
      <Rectangle ... />
      <Ellipse ... />
      <Path ... />
    </Grid>
  </DataTemplate>
</ResourceDictionary>

...
<Canvas ...>
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
</Canvas>

要在这些方式之间进行选择,请决定您想要哪些附加功能(如果有)。您可能希望向控件或数据对象添加属性。

  • 如果您使用 ControlTemplate,您的自定义控件可以参与属性继承并成为可视化树的一部分,接收所有事件。您还可以在绑定中同时引用 DataContext 和 TemplatedParent,这样更加灵活。
  • 如果您使用 DataTemplate,则可以直接针对模型中的对象进行操作。

您还可以使用 ItemsControl 及其子类(ListBox、ComboBox 等)来适当地呈现您的形状,而不是列出单个控件。

替代方法

另一种完全不同的方法是将形状集合转换为 Drawing 对象,并使用 DrawingImage 或 DrawingBrush 来呈现它。

For most purposes putting them in a ControlTemplate or DataTemplate works the best. Here's the ControlTemplate way:

<ResourceDictionary>
  <ControlTemplate x:Key="MyShape">
    <Grid With="..." Height="...">
      <Rectangle ... />
      <Ellipse ... />
      <Path ... />
    </Grid>
  </ControlTemplate>
</ResourceDictionary>

...
<Canvas ...>
  <Control Template="{StaticResource MyShape}" ... />
  <Control Template="{StaticResource MyShape}" ... />
  <Control Template="{StaticResource MyShape}" ... />
  <Control Template="{StaticResource MyShape}" ... />
</Canvas>

And the DataTemplate way:

<ResourceDictionary>
  <DataTemplate x:Key="MyShape">
    <Grid With="..." Height="...">
      <Rectangle ... />
      <Ellipse ... />
      <Path ... />
    </Grid>
  </DataTemplate>
</ResourceDictionary>

...
<Canvas ...>
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
  <ContentPresenter ContentTemplate="{StaticResource MyShape}" ... />
</Canvas>

To choose between these, decide what additional functionality (if any) you want. You will probably want to add properties to your control or your data object.

  • If you go with a ControlTemplate, your custom control can participate in property inheritance and be part of the visual tree, receiving all the events. You can also refer to both the DataContext and the TemplatedParent in the bindings, which is more flexible.
  • If you go with a DataTemplate, you can work directly against objects in your model.

Instead of listing individual controls you can also use ItemsControl and its subclasses (ListBox, ComboBox, etc) to present your shapes appropriately.

Alternate approach

Another completely different approach is to convert your collection of shapes to a Drawing object and present it using a DrawingImage or a DrawingBrush.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文