Specifying a Button Content that has mix of text and a Binding path

user565660 picture user565660 · Mar 1, 2011 · Viewed 20k times · Source

How do you specify the Content of a button that is a mix of some TEXT and a Binding path?

Like this:

<Button Content= "TEXT" + "{Binding Path=ButtonContent}"

Answer

Fredrik Hedblad picture Fredrik Hedblad · Mar 1, 2011

For most cases you can use StringFormat in the Bindings, like for a TextBlock

<TextBlock Text="{Binding ElementName=textBox,
                          Path=Text,
                          StringFormat='{}{0} - Added Text'}"/>

However, this has no effect on a ContentControl (which Button inherits from). Instead, you can use ContentStringFormat

<Button Content="{Binding ElementName=textBox,
                          Path=Text}"
        ContentStringFormat="{}{0} - Added Text"/>

Also, for

  • ContentControl you use ContentStringFormat
  • HeaderedContentControl you use HeaderStringFormat
  • ItemsControl you use ItemStringFormat