18

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

4 Answers 4

25

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
No. An attribute can only hold plain text.
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.
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.
Can this be done in the same way in VS2017 ; please help as I am unable to do so
13

This will work.

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

Comments

2

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

Comments

-1

The simpliest solution i could think of:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.