devoured elysium
devoured elysium

Reputation: 105217

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

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

Upvotes: 18

Views: 57929

Answers (4)

The simpliest solution i could think of:

 private void ButtonClick(object sender, RoutedEventArgs e)
 {
     string buttonText = (sender as Button).Content.ToString();
 }

Upvotes: -1

itowlson
itowlson

Reputation: 74832

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.

Upvotes: 25

SLaks
SLaks

Reputation: 888117

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

Upvotes: 2

Steve Danner
Steve Danner

Reputation: 22168

This will work.

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

Upvotes: 13

Related Questions