Igor Adamenko
Igor Adamenko

Reputation: 143

How to group shapes in XAML?

Help me, please. I have this:

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50"/>
        <ColumnDefinition Width="50"/>
    </Grid.ColumnDefinitions>
    <Rectangle x:Name="MyObject" Fill="Red" ManipulationDelta="Object_ManipulationDelta" Height="80" Width="80" ManipulationMode="All">
        <Rectangle.RenderTransform>
            <CompositeTransform/>
        </Rectangle.RenderTransform>
    </Rectangle>
    <Rectangle Fill="Blue" Grid.Row="1">
        <Rectangle.RenderTransform>
            <CompositeTransform/>
        </Rectangle.RenderTransform>
    </Rectangle>
    <Rectangle Fill="Green" Grid.Column="1"/>
    <Rectangle Fill="Yellow" Grid.Row="1" Grid.Column="1"/>
</Grid>

And I have function Object_ManipulationDelta which change position of element if it is dragged.

  1. So, how I can group all Rectangles (or some of them; like array), and use one function Object_ManipulationDelta for change location whole group if one of them is dragged? In function I use this: var obj = (CompositeTransform)MyObject.RenderTransform and TranslateX (and Y) for obj.
  2. Is there a way to not write <Rectangle.RenderTransform... after each rectangle?

Upvotes: 0

Views: 1189

Answers (2)

Martin Suchan
Martin Suchan

Reputation: 10620

Just name the whole Grid you have mentioned in your code sample using x:Name="name" and apply the transformation on this Grid. Or group only the target rectangles in nested Grid and apply the transformation on it.

Upvotes: 1

Cyprien Autexier
Cyprien Autexier

Reputation: 1998

Any Panel such as Grid can do the grouping job. Grid has ManipulationDelta event too. To avoid duplicate Composite transform move it to a grid containing the rectangles.

Upvotes: 0

Related Questions