WPF Button Size and Text Wrapping

G

Gordon Padwick

After spending many hours familiarizing myself with WPF, I'm at the stage of
creating my first real application.

I've run into a problem of sizing buttons and wrapping text content in
buttons.

I have these questions:

1. How can I create a button that is automatically sized to accommodate its
text content (such as the SizeToContent property of a window)?

2. How can I wrap text within a WPF button (as happens in a Microsoft Access
button)?

3. How can I create a group of buttons, such as within a StackPanel (within
a Grid) so that all buittons have the same size, even when the size of one
of those buttons changes? I have in mind that I might add a button to a
group and that button might need to be larger than pre-exisiting buttons, or
that subsequent localization results in the need for a larger button.

I'll greately appreciate all responses to these questions.

Gordon Padwick
 
P

Pavel Minaev

After spending many hours familiarizing myself with WPF, I'm at the stageof
creating my first real application.

I've run into a problem of sizing buttons and wrapping text content in
buttons.

I  have these questions:

1. How can I create a button that is automatically sized to accommodate its
text content (such as the SizeToContent property of a window)?

It does that by default, if you do not specify Width & Height (as does
pretty much any ContentControl).
2. How can I wrap text within a WPF button (as happens in a Microsoft Access
button)?

If you mean auto-wrap, then you should explicitly put a TextBlock into
the button, the text you want to display inside that TextBlock, and
set its TextWrapping property as needed.

If you mean manual line breaks, then put a StackPanel inside, with
Orientation=Vertical, and into that put a TextBlock for every line you
want.

Ultimately, of course, you can always just put a
FlowDocumentScrollViewer inside a button, and arbitrarily complicated
flow markup in that, with paragraphs and whatnot. It should also wrap
as needed.
3. How can I create a group of buttons, such as within a StackPanel (within
a Grid) so that all buittons have the same size, even when the size of one
of those buttons changes? I have in mind that I might add a button to a
group and that button might need to be larger than pre-exisiting buttons,or
that subsequent localization results in the need for a larger button.

In a Grid, all controls in the same column should have the same width
with default settings - this easily covers the case of buttons stacked
up vertically.

In more general case of the grid, you should set SharedSizeGroup on
RowDefinition and ColumnDefinition objects in the grid to a single
value, so that all rows/columns get the same size. This should do the
trick for buttons laid out in a grid in a line. Also, I've never tried
to create a shared size group for both rows and columns, but if that
works, then you'll get fully interlocked size in all dimensions.

In a vertically-oriented StackPanel, all your buttons should have the
same width (which will be the width of the panel) if you set their
HorizontalAlignment=Stretch. In a horizontal StackPanel, I am not
aware of ways of making all buttons the same width.
 
G

Gordon Padwick

Thanks, Pavel, for your fast and informative answers.

Since sending my original message, I've discovered that the Visual Studio
2008 Help topic "WPF Globalization and Localization Overview" contains some
information about keeping a group of buttons the same size when the text in
one of the buttons grows.. That information is in the section headed
"Designing a Global Run Dialog Box."

Others who are interested might like to refer to this Help topic.

Gordon

After spending many hours familiarizing myself with WPF, I'm at the stage
of
creating my first real application.

I've run into a problem of sizing buttons and wrapping text content in
buttons.

I have these questions:

1. How can I create a button that is automatically sized to accommodate
its
text content (such as the SizeToContent property of a window)?

It does that by default, if you do not specify Width & Height (as does
pretty much any ContentControl).
2. How can I wrap text within a WPF button (as happens in a Microsoft
Access
button)?

If you mean auto-wrap, then you should explicitly put a TextBlock into
the button, the text you want to display inside that TextBlock, and
set its TextWrapping property as needed.

If you mean manual line breaks, then put a StackPanel inside, with
Orientation=Vertical, and into that put a TextBlock for every line you
want.

Ultimately, of course, you can always just put a
FlowDocumentScrollViewer inside a button, and arbitrarily complicated
flow markup in that, with paragraphs and whatnot. It should also wrap
as needed.
3. How can I create a group of buttons, such as within a StackPanel
(within
a Grid) so that all buittons have the same size, even when the size of one
of those buttons changes? I have in mind that I might add a button to a
group and that button might need to be larger than pre-exisiting buttons,
or
that subsequent localization results in the need for a larger button.

In a Grid, all controls in the same column should have the same width
with default settings - this easily covers the case of buttons stacked
up vertically.

In more general case of the grid, you should set SharedSizeGroup on
RowDefinition and ColumnDefinition objects in the grid to a single
value, so that all rows/columns get the same size. This should do the
trick for buttons laid out in a grid in a line. Also, I've never tried
to create a shared size group for both rows and columns, but if that
works, then you'll get fully interlocked size in all dimensions.

In a vertically-oriented StackPanel, all your buttons should have the
same width (which will be the width of the panel) if you set their
HorizontalAlignment=Stretch. In a horizontal StackPanel, I am not
aware of ways of making all buttons the same width.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top