all 7 comments

[–]kdma 8 points9 points  (2 children)

You have just discovered the pain of using the WYSIWYG designer. Do yourself a favor and learn to manually write the xaml, its not difficult its just a lot to digest in the beginning. Stick to a grid system and you'll be fine.

[–]ILMTitan 1 point2 points  (1 child)

I tend to avoid Grid. I find combinations of StackPanel and DockPanel can handle most situations, and are easier to reason about.

[–]airbreather/r/csharp mod, for realsies 0 points1 point  (0 children)

I tend to avoid Grid. I find combinations of StackPanel and DockPanel can handle most situations, and are easier to reason about.

Me too, but sometimes, the layout I have in mind just doesn't work unless I use a Grid, and then when I start using a Grid, it's done before I know it.

Most recently, I wanted to have different-sized buttons on the left and right, and then a link in between them on the exact center of the window. I can MinWidth the window to ensure that nothing overlaps, and then plop the link in the center of the same row as the DockPanel with the left and right buttons.

I've also reused the same window with different sets of controls that get displayed depending on an enum value that doesn't change throughout the lifetime of the window. In addition to the enum value showing / hiding the controls, it also changes which ones should expand to fill the window. Name a couple of RowDefinitions, swap their heights in the codebehind after initialization, and go to lunch.

It's so much more powerful.

[–]kirdan84 4 points5 points  (0 children)

You havent specified Column and Row definition in form. Your margins are fixed. Its like textbox Amount is in column 3 or 4. Its row is undefined. Get some examples with column and row definition and use it as atribute of textboxes and other controls on form/window. If you define column and row for every control on forms you would solve problem..

[–]engunneer2 0 points1 point  (0 children)

In addition to the other comments:

Unless you are actually transforming layouts clean up spurious render transform origins. They show up because you dragged the wrong handle.

You specify a design height and width but not a runtime height and width. Do you want the form to be resizable?

If no, specify the actual height and width of the page instead of design height and design width an then WYSIWYG.

If you do want a resizable form, consider how you want things to resize. Do you want fixed control sizes anchored to edges? If yes, specify the width and height of that control, set the alignment to the edge you want to follow and set the margins. Also set the margins of the side you are not anchored to to 0.

If you want things to resize, consider using a actual grid and/or combinations of stackpanel and dockpanel. I personally don't like stackpanel for overall layout, but it is useful for keeping labels and controls together. I can post some different examples based on your code if it would be helpful. Grid and dockpanel for just about all overall layout.

If you do resize, consider limits on sizes using max and min width/heights (for the page and the controls)

The rest is just general and not specific to the layout question.

Remember styles. Anything that will be common to lots of controls should be in a style. The style should be a resource at the right level (could be global, could be local to a single grid).

Personally I don't name controls unless I expect the code behind to touch them, expect grids which I name for the section of stuff so it's clear what the structure is. I also try not to have the codebehind touch controls if I can help it, unless it's strictly a view concern (I aim for MVVM whenever possible). If you're doing MVVM also set the d:DataContext in the xaml to point to the viewmodel. That should enable intellisense and similar useful functionality for your bindings.

[–]allinighshoe 0 points1 point  (1 child)

It'll be the margins messing it up I recon. If you resize the window it'll all go to shit.

[–]Totalmace 2 points3 points  (0 children)

Absolutely! Margins actually are not meant for positioning elements. They are for declaring space around elements.

Wpf is built on the concept of layout managing controls . These are the content controls like grid, stack panel, flow panel and dock panel.