Adjust shape when window is resized
hi all!
i have the following code in my application which creates a rectangle and adds it to my canvas:
rect =newRectangle();
rect.Stroke =newSolidColorBrush(Colors.Red);
canvas.Children.Add(rect);
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, 10);
Canvas.SetTop(rect, 10);
what property do i have to set so that when the canvas is resized, the rectangle adjusts its size and position accordingly?
[1214 byte] By [
luth0r] at [2008-1-8]
Hi,
I don' know exactly which functionality you are looking for, but, if I were you, I would put the Canvas in a Viewbox, then you get the size and position updates for free. Or are you trying to obtain another result?
Best regards,
Benny
Dont use Width and Height, instead use Margin so the Width and Height will be resized acording to its Parent.
for example centered sizing rectangle:
Code Snippet
rect.Margin.right = rect.Margin.left = new Thickness(canvas.Width / 2 - 25,
canvas.Height / 2 - 25,
canvas.Width / 2 - 25,
canvas.Height / 2 - 25 );
Don't put the Rectangle in a Canvas. Put it in a Grid cell or even just set it to the content of a Page or Window. Don't set the Width and Height. By default, the Rectangle will automatically fill its parent because the HorizontalAlignment and VerticalAlignment properties have default values of Stretch. (See Chapter 3 of my book "Applications = Code + Markup" and later chapters for details.)