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.