• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

How can I center multiple lines of text in a WPF Button?

Posts: 1464
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[url=https://coderanch.com/t/678028/visual-basic/WPF-touchscreen-buttons-react-pressed]Elsewhere[/url], I mentioned that it is easy to label a WPF Button with more than one line of text. However, the trick I used to do it left-justified the text, which isn't how I will always want it. I also mentioned that I didn't know how to center it, but made the tantalizing comment that I might, "come back another day," pursue it.

Today's that day. The question is, "How can I center multiple lines of text in a WPF Button?" I'm sure there are many answers, some likely better than what I'll venture here. But, the XAML below does seem to work. It is based on the fact that a WPF Button can contain all sorts of things, not just text. The picture below shows five XAML Buttons, but only the first two have plain text as their content:

Here's the XAML that defines those Buttons: [code=xml] [/code]

At Line 10, I have again used my crude trick of embedding a DOS newline in the text. The first Button displays the two lines, left-justified. Note, however, that the "equivalent" XAML at Lines 12-14 [i]doesn't work[/i]. The second Button is back to using one line. So, the newline "method" is not recommended.

Here's where we stop using text as the content of a Button altogether. For the third Button, defined at Lines-16-21, we replace the text as the Button's content with a StackPanel element. Nested inside that, we include two TextBlock elements as the StackPanel's content. Finally, the content (somewhat confusingly set with the "Text" property) of those TextBlock's is set to the original two lines of text we used in the previous Buttons. The result, in the third Button down from the top, is the left-justified text we had in the first place. To center it, we simply add the HorizontalAlignment property to each TextBlock and assign the value of "Center." That's how the fourth Button is defined at Lines 23-28 (with the TextBlocks at Lines 25 and 26).

Note that quite a wide variety of elements can serve as the content of a WPF Button, beyond text and TextBlocks. For example, the fifth Button is defined by Lines 30-32. Its content is an Image element, the source of which is actually a URL, with the image file residing somewhere on the Web. The wisdom of using Web resources for UI controls is debatable, but Line 31 does, I think, a striking job of illustrating how far a WPF Button's contents can be from plain text, and how easily the required XAML code can cross that distance.

Note also that this post, which I have originated in the Visual Basic forum, contains no Visual Basic. As per the WPF design goal of being usable in all .NET languages, the XAML here would work just as well in, say, a C# context as it does in a Visual Basic context (hence, I am adding the C# forum to this posting).

I'm reading Adam Nathan's "WPF 4.5 Unleashed," which is an excellent guide to WPF and XAML. (Its example code is all in C#, but there seems to be little effort required to mentally translate that into Visual Basic, especially if one already knows C/C++ and/or Java.) I'm not usually a fan of the "Unleashed" books, as their editorial style is often a bit unfocused, I feel. This one is different. If you are looking for a book on WPF, this is the one you are looking for.

More soon...
This will take every ounce of my mental strength! All for a tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic