How to add plain text to a TextBlock that already has a binding to the 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 selected by the element. Now let's just say that, for example, the selected item says "Hello." And I want to add my name to it (in XAML, not for the code), so it reads like this: "Hello, Arrow". How can i do this?

+6
source share
3 answers

Try the following:

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

XAML solutions are not yet available in Metro XAML:

You can use StringFormat :

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

You can also use MultiBinding and StringFormat :

 <TextBlock> <TextBlock.Text> <MultiBinding StringFormat="{}{0} Arrow."> <Binding ElementName="theList" Path="SelectedItem.Name" /> </MultiBinding> </TextBlock.Text> </TextBlock> 
+10
source

With this configuration, the only thing you can do is text in the selected item. So, I would recommend the following:

 <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 suffixText any way you want and get the results you are looking for.

+3
source

you need to create a MultiValueConverter for this, which combines two lines. You can transfer these lines from xaml to the converter. see article for more details

0
source

All Articles