I'm looking for something along these lines:
<TextBlock
Grid.Column="1"
Text="Welcome, {Binding UserName}!" />
This will of course actually display the text "{Binding UserName}" to the user rather than decode it, but I know you can do something like this with ASP.NET, so I'm hoping there's a way to get this to work in WPF.
I'm already aware that I could use an IValueConverter
...I'm looking for something I can do purely in markup if possible.
EDIT:
Based on @Matt Hamilton's most excellent solution, I attempted to push the envelope and bind two values into the same TextBlock
using a MultiBinding
. Works like a charm:
<TextBlock
Style="{StaticResource TextBlock_ValueStyle}"
Grid.Column="1">
<TextBlock.Text>
<MultiBinding
StringFormat="{}Attempts: {0:G} of {1:G}">
<Binding
Path="AttemptNumber" />
<Binding
Path="AttemptCount" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
This produces: Attempts: 1 of 4
(assuming AttemptNumber = 1
and AttemptCount = 4
).
I also found this link helpful for figuring out which formats to place after the colon:
http://msdn.microsoft.com/en-us/library/fbxft59x.aspx
Adding Line Breaks You can do this with a LineBreak inline, added as an XAML element within the text. A new line will be started at the position of this element. If you have configured the text to be justified, the final line before the line break will not be expanded to fill the available width.
Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.
Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.
The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.
You can use the StringFormat binding property in .NET 3.5 SP1:
<TextBlock Text="{Binding UserName,StringFormat='Welcome, \{0\}!'}" />
Note that you need to escape the curly braces in the string format with a backslash.
Update Yes, multiple values are also supported:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Welcome, {0} {1}!">
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With