15,880,134 members
5.00/5 (1 vote)
See more:
Hi all,

iam able to transform single shape at a time but how can we transform multiple shapes at the same time. I hope we can do this in WPF.

I have two buttons, first button will create rectangle shape, second button i want to transform the shape from one location to another location;

Code:
```void Button1_Click(object sender, RoutedEventArgs e)
{
Point pt=new Point(100,90);//some point
rect =new Rectangle
{
Stroke=Brushes.Blue,
StrokeThickness=1
};
rect.Width=90;
rect.Height=60;
rect.Fill =Brushes.Coral;
Canvas.SetLeft(rect,pt.X);
Canvas.SetTop(rect,pt.Y);
}

void Button2_Click(object sender, RoutedEventArgs e)
{
GeneralTransform myTransform=rect.TransformToDescendant (rect);
Point pt=myTransform .Transform (new Point(100,50));
Canvas.SetLeft(rect,pt.X-rect.Width/2);
Canvas.SetTop(rect,pt.Y-rect.Height/2);
}```

each time when i click on button1 rectangle shape is created. and when i click on button 2 only one rectangle is transforming.

I want multiple rectangles transform to another location.
plz some one give me idea to transform multiple shapes at a time.

Thanks,
Posted
Updated 2-Nov-11 23:54pm
v2

## Solution 1

First, define a List destined to hold all the rectangles you create on Button1 press

```List<rectangle> rectList = new List<rectangle>();
</rectangle></rectangle>```

Don't copy the last two /rectangle with the brackets, I gave up trying to make the editor display it appropriately...

Then:

```private void Button1_Click(object sender, RoutedEventArgs e)
{
Point pt = new Point(100, 90);//some point
Rectangle rect = new Rectangle
{
Stroke = Brushes.Blue,
StrokeThickness = 1
};
rect.Width = 90;
rect.Height = 60;
rect.Fill = Brushes.Coral;
Canvas.SetLeft(rect, pt.X);
Canvas.SetTop(rect, pt.Y);

}

private void Button2_Click(object sender, RoutedEventArgs e)
{
foreach (Rectangle rect in rectList)
{

GeneralTransform myTransform = rect.TransformToDescendant(rect);
Point pt = myTransform.Transform(new Point(100, 50));
Canvas.SetLeft(rect, pt.X - rect.Width / 2);
Canvas.SetTop(rect, pt.Y - rect.Height / 2);

}
}```

v7