如何将元素放置到Silverlight的位置?

时间:2009-11-12 12:53:36

标签: c# silverlight layout

我想绘制矩形来定位从startPoint得到的东西,但现在它将我的矩形放在PictureCanvas的中间,当我想把它放在startPoint位置

private void DragSelectComponent_SelectionEnd(DragSelectEventArgs e)
{
    Output.Text = "Start: " + e.StartPoint.ToString() + "     End: " + e.EndPoint.ToString() + "\n(" + e.EventType + ")\n";
    Rectangle rectangle = new Rectangle() { Width = e.EndPoint.X - e.StartPoint.X, Height = e.EndPoint.Y - e.StartPoint.Y, Fill = new SolidColorBrush(Colors.Purple) };
    PictureCanvas.Children.Add(rectangle);
}

2 个答案:

答案 0 :(得分:1)

你试过了吗?

rectangle.SetValue(Canvas.LeftProperty, p.X);
rectangle.SetValue(Canvas.TopProperty, p.Y);

答案 1 :(得分:1)

只需使用: -

 Rectangle rectangle = new Rectangle() { Width = e.EndPoint.X - e.StartPoint.X, Height = e.EndPoint.Y - e.StartPoint.Y, Fill = new SolidColorBrush(Colors.Purple) };
 Canvas.SetLeft(rectangle, e.StartPoint.X);
 Canvas.SetTop(rectangle, e.StartPoint.Y);
 PictureCanvas.Children.Add(rectangle);