Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WPF how to set location,width and height of the controls programmatically?

I'm doing my first WPF application. im having problem whereby when my form is maximized or fullscreen, my controls wont resize and just stay in the same location. only the form is maximized.

in winform, I can do the adjustment in the .cs like the following:

panel6.Width
panel6.Height
panel6.Location

this will help me set it if my form is maximized. I do it by using some arithmetics where I get the resolution of screen and some calculation and get the value and can set it to the width, height and location. BUT THIS IS IN WINFORM.

how will I tackle this issue for maximized and fullscreen in WPF? is there a way to be done through the .cs file programmatically? or does WPF come with a easy built in control to tackle this issue?

suppose for this example I'm using dockpanel in the WPF.

it will be pointless if window is maximized but the other controls remains.

like image 648
Psychocryo Avatar asked May 10 '11 08:05

Psychocryo


2 Answers

To set Width and Height:

dockpanel1.width = 230;
dockpanel1.height = 230;

as for location, wpf uses Margin:

dockpanel1.Margin = new Thickness(0,440,836,40);
like image 95
Psychocryo Avatar answered Sep 28 '22 00:09

Psychocryo


It is possible to programmatically move child elements on a Canvas.

In xaml:

<Canvas>
    <YourElement Canvas.Top="x" Canvas.Left="y"/>
</Canvas>

In C#:

Canvas.SetTop(YourElement, newX);
Canvas.SetLeft(YourElement, newY);
like image 21
Roger Lew Avatar answered Sep 27 '22 23:09

Roger Lew