views:

394

answers:

2

I want to draw rectangle to position what I get from startPoint, but now it places my rectangle to middle of PictureCanvas, when I want to put it in startPoint position

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);
}
+1  A: 

Have you tried:

rectangle.SetValue(Canvas.LeftProperty, p.X);
rectangle.SetValue(Canvas.TopProperty, p.Y);
ChrisF
I tried that, but it doesnt seem to work.
newbie
+1  A: 

Just use:-

 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);
AnthonyWJones
It didn't work, for some reason my rectangle is still in the middle of PictureCanvas
newbie
You are sure that "PictureCanvas" is actually a Canvas? Are you sure that the rectangle in the event args __doesn't__ describe a rectangle in the middle of the Canvas?
AnthonyWJones
PictureCanvas is Grid element. should it be Canvas element?
newbie
<Grid Width="500" x:Name="PictureCanvas" Height="500" Background="Orange" Grid.Column="1"></Grid>
newbie
The Canvas Left and Top properties only make sense when you place the item on a Canvas.
AnthonyWJones