在WPF中一次转换多个形状 [英] Transforming multiple shapes at a time in WPF
问题描述
大家好,
我可以一次转换单个形状,但是我们如何一次转换多个形状.我希望我们可以在WPF中做到这一点.
我有两个按钮,第一个按钮将创建矩形形状,第二个按钮我想将形状从一个位置转换为另一个位置;
代码:
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);
canvas1.Children.Add(rect);
}
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);
}
每次我单击button1时,都会创建矩形形状.当我单击按钮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,
推荐答案
首先,定义一个列表,该列表用于保存在Button1上创建的所有矩形,按
First, define a List destined to hold all the rectangles you create on Button1 press
List<rectangle> rectList = new List<rectangle>();
</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);
canvas1.Children.Add(rect);
rectList.Add(rect);
}
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);
}
}
这篇关于在WPF中一次转换多个形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!