Prev | Current Page 213 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"


When setting margins, Visual Studio uses the closest corner. For example, if your element is nearest to
the top-left corner of the Grid, Visual Studio pads the top and left margins to position the element (and leaves
the right and bottom margins at 0). If you drag your element down closer to the bottom-left corner, Visual
Studio sets the bottom and left margins instead and sets the VerticalAlignment property to Bottom. This
obviously affects how the element will move when the Grid is resized.
Visual Studio??™s margin-setting process seems straightforward enough, but most of the time it won??™t create
the results you want. Usually, you??™ll want a more flexible flow layout that allows some elements to expand
dynamically and push others out of the way. In this scenario, you??™ll find that hard-coding position with the
Margin property is extremely inflexible. The problems get worse when you add multiple elements, because
Visual Studio won??™t automatically add new cells. As a result, all the elements will be placed in the same cell.
Different elements may be aligned to different corners of the Grid, which will cause them to move with
respect to one another (and even overlap each other) as the window is resized.
Once you understand how the Grid works, you can correct these problems. The first trick is to
configure your Grid before you begin adding elements by defining its rows and columns.


Pages:
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225