WPF StringFormat on Label Content
WpfWpf ControlsBindingWpftoolkitWpfdatagridWpf Problem Overview
I want to format my string binding as Amount is X
where X
is a property bound to a label.
I've seen many examples but the following doesn't work:
<Label Content="{Binding Path=MaxLevelofInvestment,
StringFormat='Amount is {0}'}" />
I've also tried these combinations:
StringFormat=Amount is {0}
StringFormat='Amount is {}{0}'
StringFormat='Amount is \{0\}'
I even tried changing the binding property's datatype to int
, string
and double
. Nothing seems to work. This is a very common use case but doesn't seem to be supported.
Wpf Solutions
Solution 1 - Wpf
The reason this doesn't work is that the Label.Content
property is of type Object
, and Binding.StringFormat
is only used when binding to a property of type String
.
What is happening is:
- The
Binding
is boxing yourMaxLevelOfInvestment
value and storing it theLabel.Content
property as a boxed decimal value. - The Label control has a template that includes a
ContentPresenter
. - Since
ContentTemplate
is not set,ContentPresenter
looks for aDataTemplate
defined for theDecimal
type. When it finds none, it uses a default template. - The default template used by the
ContentPresenter
presents strings by using the label'sContentStringFormat
property.
Two solutions are possible:
- Use Label.ContentStringFormat instead of Binding.StringFormat, or
- Use a String property such as TextBlock.Text instead of Label.Content
Here is how to use Label.ContentStringFormat:
<Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}" />
Here is how to use a TextBlock:
<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" />
Note: For simplicity I omitted one detail in the above explanation: The ContentPresenter
actually uses its own Template
and StringFormat
properties, but during loading these are automatically template-bound to the ContentTemplate
and ContentStringFormat
properties of the Label
, so it seems as if the ContentPresenter
is actually using the Label
's properties.
Solution 2 - Wpf
Make a universal StringFormatConverter : IValueConverter
. Pass your format string as ConverterParameter
.
Label Content="{Binding Amount, Converter={...myConverter}, ConverterParameter='Amount is {0}'"
Also, make StringFormatMultiConverter : IMultiValueConverter
when you need more than one object in format string, for instance, Completed {0} tasks out of {1}
.
Solution 3 - Wpf
I just checked and for some reason it doesn't work with the Label, probably because it uses a ContentPresenter for the Content property internally. You can use a TextBlock instead and that will work. You could also put the TextBlock excerpt below in the content of a Label if you need to inherit styling, behaviour etc.
<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is \{0\}'} />
Solution 4 - Wpf
Try using a converter....
<myconverters:MyConverter x:Key="MyConverter"/>
<Label Content="{Binding Path=MaxLevelofInvestment, Converter={StaticResource MyConverter"} />
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return String.Format("Amount is {0}", value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
Solution 5 - Wpf
You can use this below
<Label Content="{Binding Content, StringFormat='Page Data> {0}'}" />
"Content" is a binding variable and between the single quotes type your text. {0} where the Content data will be inserted.