开发者

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

开发者 https://www.devze.com 2023-02-14 00:46 出处:网络
How do you specify the Content of a button that is a mix of some TEXT and a Binding path? Like this: <B开发者_如何学Cutton Content= \"TEXT\" + \"{Binding Path=ButtonContent}\"

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

Like this:

<B开发者_如何学Cutton Content= "TEXT" + "{Binding Path=ButtonContent}"


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


Something like this:

<Button>
   <Button.Content>
      <TextBlock Text="{Binding SomeBindingPath, StringFormat='Some text {0}'}"/>
   </Button.Content>
</Button>

OR

<Button>
   <Button.Content>
      <StackPanel Orientation="Horizontal">
         <TextBlock Text="Some Text"/>
         <TextBlock Text="{Binding SomeBindingPath}"/>
      </StackPanel>
   </Button.Content>
</Button>

Basically, you can put any content inside a button using the approach above.


Building on the other answers, this is a little more terse:

<Button Content="{Binding FirstName, StringFormat='Click here, {0}!'}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消