I’m trying to dynamicaly add a shape to a grid, I’m creating and setting it this way :
Rectangle theRect = new Rectangle();
currentRect = theRect;
theRect.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
theRect.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
theRect.Margin = new Thickness(oldPos.X,oldPos.Y,0,0);
theRect.StrokeThickness = brushWidht;
theRect.Stroke = new SolidColorBrush(brushColor);
theRect.Height = newPoint.Y - oldPos.Y;
theRect.Width = newPoint.X - oldPos.X;
theBoard.Children.Add(theRect);
But it sticks to the corner of “theBoard” which is the grid where I put it.
Does anybody can help me with this ?
Thanks.
Since you’ve decided to have a
Gridfor the parent, I suppose you’re trying to put your shape in different “cells”, i.e. in different rows and columns. You can do that with the following code:If you want to set the absolute position of the shape in the parent then a
Canvaswould be a better choice. In this case you could set the offset of the shape inside the parent using the following code:I hope this answers your question.