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]
# 1
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

BennyTops at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
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 );


SvenHecht at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3
what i am trying to accomplish is somewhat similar to wikimapia. where the user defines an "area" and provide a description of that area. what the user does is click and drag the mouse to define an area, and i create the rectangle object based on the mouse coordinates. my problem is, when the user resizes the window, the background image stretches (it is set to FILL), but the rectangle retains its position. i'm a neewbie in WPF and i dont know what property to set. i tried putting the canvas inside a viewbox but everything turns white and the background image is gone when i do that.
luth0r at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

As we known, Canvas layout its children control by absolute position. so you can binding width property of Rectangle to Canvas's ActucalWidth.

YilingLai at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

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.)

CharlesPetzold at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified