2

I have just started getting into learning WPF, and to add controls to the window I just move them from the toolbox and onto the window. Then I can continue moving them to put them in the places I want. Now I have got into layouts/panels, stack panels, canvas,dock panels etc and I am struggling to understand why they are useful if you can just drag and move objects/controls on the screen yourself.

For example dock panels, using the dock property you can put the object on the left,right,bottom,top,lastchildfill. You can dock a textbox to the left by using the property, DockPanel.Dock = "Left", but why can't I just move it there myself?

EKrueger
  • 730
  • 8
  • 20
Foysal94
  • 565
  • 2
  • 7
  • 15
  • 1
    Yes, You can, but take note it can behave differently when You resize the window. Take a look at description of different containers here: http://msdn.microsoft.com/en-us/library/ms754152%28v=vs.110%29.aspx#Panels_derived_elements and here: http://www.codeproject.com/Articles/140613/WPF-Tutorial-Layout-Panels-Containers-Layout-Trans#heading0005. – Lukasz M Jun 24 '14 at 16:23
  • The `Panel` container allows your layouts to be much more flexible with dynamic sizing and content. With a `StackPanel` for example, you could easily add content to it from code. Panels also allow for easier alignment options (Center, Top, etc.) that will perfectly align your controls for you. Why do it yourself! :) – Chris Badman Jun 24 '14 at 16:25

2 Answers2

1

First off, don't even waste time drag/dropping controls into a WPF app. By placing controls in this manner, you are severely limiting your design potential in WPF. Most serious WPF designers don't even bother dragging controls, they hand type all of the XAML. Arguably, this is how WPF was designed. Leveraging XAML allows your layouts to be totally dynamic in ways that WinForms could never dream of, but you have to hand code the XAML.

Do you see where this is going? Don't drag/drop controls when designing WPF apps! Every conference I've been to stresses this fact! Grid panels are a huge part of WPF layouts, and are crucial to getting your controls to end up where you expect.

The hardest part about learning WPF is figuring out it is not WinForms with nice graphics. It is a totally different beast, with a steep learning curve! Hand coding XAML is extremely tedious at first, but once you learn the names of all the controls and the important properties, you'll be cranking out UIs way faster than the old drag/drop method.

Lee Harrison
  • 2,306
  • 21
  • 32
  • I totally agree with you. I've worked on a project in WPF, and we never ever used the designer. Everything was coded in XAML. Combined with a MVVM pattern, WPF development was one of the best frameworks I've had to work with. – gretro Jun 24 '14 at 16:32
0

You can move the controls manually, but different layout panels can automatically handle controls positioning when You add new controls or when the window is resized.

Please take a look at description on MSDN and on CodeProject that describes different types of containers.

Lukasz M
  • 5,635
  • 2
  • 22
  • 29