17

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.

1
  • 2
    As people are mentioning the layout elements in WPF, would be worth mentioning that you can use anchoring and docking in WinForms as well to make your life easier.
    – Jaymz
    May 10, 2011 at 9:04

6 Answers 6

34

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);
1
16

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);
1
  • This is a more intuitive way to simply place controls and move them. Margin seems to set things relative to other elements. Its much harder.
    – Tim
    Aug 24, 2016 at 10:06
2

Use some calculations like (control's previous position * layout's new size) / layout's previous size = control's new position

But the easiest way is to use XAML Use Grid and put columns and rows in it and set the size of columns and rows to * So on layout size change, your controls will reposition in refer to parent's change in size which your grid is child of it. And you could even have auto resizable controls just by setting the controls' margins in columns and rows. Don't forget horizontal and vertical alignments set to stretch.

1

For your specific problem, would recommend you to use a DockPanel and place your controls in it. Here's first bing result: WPF Tutorial | Dock Panel

And as already suggested by flq and blindmeis, do study the layout panels. That will make your life really simpler in WPF.

0
0

you should look at all the different layoutpanels in wpf (Grid, DockPanel, StackPanel, Canvas ...)

so if you set any properties for your Panel, it behaves different if your LayoutPanel change.

0

If you want explicitly position your child items use Canvas panel

<Canvas>
    <YourElement Canvas.Top="x" Canvas.Left="y"/>
</Canvas>
1
  • you can..but if someone want to change the position dynamically and not with XAML, there is no way to do this one :].. Sep 26, 2012 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.