Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append regular text to a TextBlock that already has a Binding to its text property?

I have a TextBlock:

<TextBlock x:Name="someText" Text="{Binding ElementName=theList, Path=SelectedItem.Name, Mode=TwoWay}" />

And as you can see, it is bound to another element's selected item. Now, let's just say that, for example, the selected item says "Hello, ". And I want to append my name to it (in XAML, not code-behind), so that it reads like: "Hello, Arrow.". How can I do this?

like image 390
Arrow Avatar asked Dec 27 '12 03:12

Arrow


2 Answers

Try this:

<TextBlock x:Name="someText" TextWrapping="NoWrap">             
   <Run Text="{Binding ElementName=theList, Path=SelectedItem, Mode=TwoWay}" />
   <Run Text=" Arrow." />
</TextBlock>

XAML solutions not available yet on Metro XAML:

You can use StringFormat:

<TextBlock x:Name="someText" Text="{Binding ElementName=theList, Path=SelectedItem, Mode=TwoWay, StringFormat={}{0} Arrow.}" />

Also you can use MultiBinding and StringFormat:

<TextBlock>
     <TextBlock.Text>
          <MultiBinding StringFormat="{}{0} Arrow.">
               <Binding ElementName="theList" Path="SelectedItem.Name" />                    
          </MultiBinding>
     </TextBlock.Text>
</TextBlock>
like image 129
kmatyaszek Avatar answered Oct 09 '22 10:10

kmatyaszek


With this configuration the only thing you could do is have that text in the selected item. So, what I would recommend is something more along these lines:

<StackPanel Orientation="Horizontal">
    <TextBlock x:Name="someText"
        Text="{Binding ElementName=theList,
                       Path=SelectedItem.Name,
                       Mode=TwoWay}" />
    <TextBlock x:Name="suffixText"/>
</StackPanel>

With this configuration you can provide the suffixText any way you want and get the results you're looking for.

like image 32
Mike Perrenoud Avatar answered Oct 09 '22 12:10

Mike Perrenoud