Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add more characters after a binding in xaml?

Tags:

I was wondering something, and couldn't find any relevant topics. I have following binding :

Content="{x:Static resx:Resource.Form_OtherOption_Description}" 

This will place a string in a label. What i was asking myself is if i can add a ":" after that binding, not in code, just in xaml. The label represent something like "Name :". But adding the ":" as part of the binding is not an option.

Edit

I'm working in 3.5 version

Any suggestions.

Thanks in advance.

like image 769
Terry Avatar asked Feb 09 '11 16:02

Terry


People also ask

How does binding work in XAML?

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.

How does binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How many types of binding are there in WPF?

WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise. OneWay: The target property will listen to the source property being changed and will update itself.

What is two way binding WPF?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.


2 Answers

You could accomplish this with something like:

<TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description},                          StringFormat={}{0}:}" /> 

Edit: <Label>s Content property does not respect the StringFormat property of a binding apparently. Which I've found has been moved to the ContentStringFormat property on the <Label>.

<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"        ContentStringFormat="{}{0}:" /> 
like image 53
user7116 Avatar answered Sep 23 '22 08:09

user7116


If you're using WPF 4.0, you could also do this:

<TextBlock>        <Run Text="{Binding SomeLabel}"/>        <Run Text=":"/> </TextBlock> 

This actually concatenates the two strings coming from two Run tag and copied into TextBlock.Text property!.

Using this approach you can even bind to different properties in presenter, and display it in a single TexBlock. See this excellent example:

Can we concat two properties in data binding?

like image 29
Nawaz Avatar answered Sep 22 '22 08:09

Nawaz