As discussed earlier, explicit sizes limit the flexibility of your user interface. In many cases, it??™s better to
let controls size themselves to fit their content or size themselves to fit their container. In the current example,
fixed sizes are a reasonable approach to give the buttons a consistent width. However, a better approach
would be to let the largest button size itself to fit its content and have all smaller buttons stretch themselves
to match. (This design, which requires the use of a Grid, is described later in this chapter.) And no matter
what approach you use with the button, you almost certainly want to remove the hard-coded Width and
Height values for the StackPanel, so it can grow or shrink to fit the available space in the window.
By default, a StackPanel arranges elements from top to bottom, making each one as tall as
is necessary to display its content. In this example, that means the labels and buttons are sized
just large enough to comfortably accommodate the text inside. All elements are stretched to
the full width of the StackPanel, which is the width of the window. If you widen the window,
the StackPanel widens as well, and the buttons stretch themselves to fit.
The StackPanel can also be used to arrange elements horizontally by setting the
Orientation property:
Now elements are given their minimum width (wide enough to fit their text) and are
stretched to the full height of the containing panel.
Pages:
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205