views:

79

answers:

1

I currently try to create classes for a paint-like WPF application. I have to base classes LineMovement (line from StartPoint to EndPoint) and PathMovement (line going through all points specified in a property Points of type PointCollection). These classes inherit from Control and get their looks through a ControlTemplate.

Now I want to add (what I call) PointMovers to the ControlTemplate. These should be little visual elements residing on each of the points in either of the Movement classes. They should become a kind of grip mechanism to drag the underlying point around.

The problem is of course that I don't know a way to create a variable number of elements in a ControlTemplate. It would be cool if I could do something like this:

<Style x:Key="{x:Type mov:PathMovement}" TargetType="{x:Type mov:PathMovement}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type mov:PathMovement}">
                <Grid>
                    <Polyline Points="{TemplateBinding Points}" />
                    <!-- interesting part start -->
                    <foreach loopvariable="Point" in="{TemplateBinding Points}">
                        <PointMover Point="Point" />
                    </foreach>
                    <!-- interesting part end -->
                </Grid>
           </ControlTemplate>
       </Setter.Value>
  </Setter>
</Style>

Is this possible? Do you have another approach in mind that could work?

Thanks in advance!

+1  A: 

Use an ItemsControl in conjunction with an ItemTemplate:

<ItemsControl ItemsSource="{Binding Points}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- rendered for each point -->
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

By the sounds of your use case, you may also want to read up on AdornerLayers.

HTH, Kent

Kent Boogaart
Looks good. I'm gonna try that. However regarding your advice with the AdornerLayers: I read there is no way to specify them in XAML so I won't use it (to not mix XAML and code more than absolutely needed).
chrischu