I'm currently using the TextBlock
below to bind the value of a property named Name
:
<TextBlock Text="{Binding Name}" />
Now, I want to bind another property named ID
to the same TextBlock
.
Is it possible to bind two or more values to the same TextBlock
? Can it be done with simple concatenation, like Name + ID
and, if not, how else could this be approached?
Use a ValueConverter
and in the markup
. . .
I know this is a way late, but I thought I'd add yet another way of doing this.
You can take advantage of the fact that the Text property can be set using "Runs", so you can set up multiple bindings using a Run for each one. This is useful if you don't have access to MultiBinding (which I didn't find when developing for Windows Phone)
You can use a
MultiBinding
combined with theStringFormat
property. Usage would resemble the following:Given a value of Name of
Foo
and a value for ID of1
you output in the TextBlock would beFoo + 1
.Note:
that this is only supported in .NET 3.5 SP1 and 3.0 SP2 or later.If these are just going to be textblocks (and thus one way binding), and you just want to concatenate values, just bind two textblocks and put them in a horizontal stackpanel.
That will display the text (which is all Textblocks do) without having to do any more coding. You might put a small margin on them to make them look right though.