Setting a button's text to have some bold characters in WPF

48,654

Solution 1

Use a TextBlock to hold the formatted text:

<Button>
  <TextBlock>Hey <Bold>you</Bold>!!!</TextBlock>
</Button>

Per your comment, if you want to be explicit about the fact that this sets the Content property, you can use XAML property element syntax to do so:

<Button>
  <Button.Content>
    <TextBlock>Hey <Bold>you</Bold>!!!</TextBlock>
  </Button.Content>
</Button>

However this is redundant because Button has a ContentPropertyAttribute which makes the first version exactly equivalent to the second anyway.

Solution 2

This will work.

<Grid>
   <Button Name="button1" Width="40" Height="40" 
           Content="something" FontWeight="Bold" />
</Grid>

Solution 3

Try <Button><TextBlock>a<Bold>b</Bold>c</TextBlock></Button>.

Share:
48,654
devoured elysium
Author by

devoured elysium

Updated on December 16, 2020

Comments

  • devoured elysium
    devoured elysium over 3 years

    I'd like to know if it is possible to define as the text of a Button in WPF, something like: a b c

    I've tried setting alt text http://img651.imageshack.us/img651/1838/ctldhrzhy41gbrcch4dpjz4.png

    but that doesn't seem to work.

    Is it only possible to use the Bold tag with FlowDocuments?

    Thanks

  • devoured elysium
    devoured elysium about 14 years
    Perfect! That'll do the job. Now, is any way to put it inside Contents? Not that I need it, just trying to understand what is going on here.
  • SLaks
    SLaks about 14 years
    No. An attribute can only hold plain text.
  • Rob Allen
    Rob Allen about 14 years
    The only way to do what you want is as posted. When you use the Content property, its the same as setting the text in the old WinForms.
  • user6170001
    user6170001 about 14 years
    Yes, but only using XAML property element syntax. I've updated the answer to show how, but to reiterate, it's redundant to do so: a child element of a Button automatically becomes its Content anyway.
  • Sujay Ghosh
    Sujay Ghosh over 6 years
    Can this be done in the same way in VS2017 ; please help as I am unable to do so