Newbie: Best Way To Design WPF Windows? RRS feed

  • Question

  • Hello, I have been programming now for several years in both asp.net and Windows Forms. I was excited to discover WPF and so I began creating applications in it recently. Thus far this has worked out very well for me. I am able to leverage all of my .net experience, and the layout tools such as Grid and StackPanel seem to work much like programming a webpage (I LIKE IT).

    But thus far I have run into some design problems. At first I thought it would be a great idea to just program my WPF windows much in the same way that I program web pages. It just seemed to make sense. I wanted my WPF windows to just "fit" the content. I soon discovered, however, that this was going to be quite a challenge!

    I tried leaving out the height and width values for my windows, and for other controls within - but it seems that WPF is just inserting somekind of value instead of letting my windows just "fit" the content within them. As an example, in one window, if I put some controls into it and just ran it - without a height value - WPF makes it very tall. It does not fit the content, and it looks ridiculous.

    After trying several fixes without success, and after becoming frustrated, I decided to adopt a windows forms approach in that I assigned height and width values to windows and some other controls. Although that was a lot more work, it seemed to work pretty well...until I tried running the app on a different machine that had Windows Vista instead of Windows XP.

    For some reason now, on Windows Vista, the content in all of my windows takes up more space. The result is that I have several windows with content that is cut off at the bottom. Again, this looks ridiculous!

    So guys, what am I doing wrong here?

    I feel that my first approach (allowing it all to just fit) would have worked, but I could never get it to look right (I made a screenshot, and then scrubed some content out. the problem is with the window height - see this link). I could only accomplish that with absolute height values. Any ideas on how to get this to work? Any suggestions on how best to proceed with my layouts?

    Monday, September 22, 2008 3:00 PM


  • The approach to keep the height and width to "Auto" is good. Use grid as much as to adjust your layout. You had a problem that your window and the content does not fit, probably you are looking for a SizeToContent property of window. SizeToContent can be set to width or height or both whatever way your window looks cool. 

    If you have any other problem than you can share your xaml. I hope this helps you. 
    • Marked as answer by Zhi-Xin Ye Thursday, September 25, 2008 6:13 AM
    Monday, September 22, 2008 6:16 PM