silverlight创建带有按钮单击的矩形 [英] silverlight create rectangle with button click
本文介绍了silverlight创建带有按钮单击的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想在C#中创建Silverlight应用程序,我添加了一个按钮,并且想一次单击一次创建一个矩形,现在我设法创建了一个矩形,但是问题是我不知道如何更改其位置下一个要创建的矩形?
请帮助我是否希望每次单击都在另一个旁边创建矩形?!
谢谢
这是代码:
C#
Hi all,
I want to create silverlight application in C#, I added a button and I want to create rectangles with each click one by one, now i managed to create one rectangle but the problem is that i don''t know how to change the location of the next rectangle to be created?
Please Help I want each click create rectangle next to another?!
Thanks
here is the code:
C#
public MainPage()
{
InitializeComponent();
Canvas c = new Canvas();
Button b = new Button();
Canvas.SetLeft(b, -50);
Canvas.SetTop(b, 20);
b.Width = 75;
b.Height = 23;
LayoutRoot.Children.Add(b);
b.Click += new RoutedEventHandler(b_Click);
}
void b_Click(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
Rectangle rectangle = new Rectangle();
rectangle.Fill = new SolidColorBrush(Colors.Cyan);
Canvas.SetLeft(rectangle, 100);
Canvas.SetTop(rectangle, 100);
rectangle.Width = 200;
rectangle.Height = 100;
LayoutRoot.Children.Add(rectangle);
}
xaml:
xaml:
<canvas x:name="LayoutRoot" removed="White" height="215" width="292" xmlns:x="#unknown">
</canvas>
推荐答案
您可以编写c#代码以根据前一个矩形设置下一个矩形.修改后的代码为:
You can write c# code to set the next rectangle based on previous one. Modified code will be:
static double prevLoc = 0;
void b_Click(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
Rectangle rectangle = new Rectangle();
rectangle.Fill = new SolidColorBrush(Colors.Cyan);
rectangle.Width = 200;
rectangle.Height = 100;
rectangle.SetValue(Canvas.LeftProperty, prevLoc);
prevLoc += 25;
LayoutRoot.Children.Add(rectangle);
}
这篇关于silverlight创建带有按钮单击的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文