Composite Form WPF

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

I was thinking of creating a UserControl, but what's the best way to encapsulate this composite form?

+3
source share
1 answer

For most purposes, using them in a ControlTemplate or DataTemplate works best. Here's the ControlTemplate path:

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

<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 them, determine what additional functionality (if any) you want. You probably want to add properties to your control or to your data object.

  • ControlTemplate, , . DataContext, TemplatedParent , .
  • DataTemplate, .

ItemsControl (ListBox, ComboBox ..), .

Drawing DrawingImage DrawingBrush.

+1

Source: https://habr.com/ru/post/1748513/


All Articles