WPF StringFormat on shortcut content

I want to format the string binding as Amount is X , where X is the property associated with the label.

I have seen many examples, but the following does not work:

 <Label Content="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" /> 

I also tried these combinations:

 StringFormat=Amount is {0} StringFormat='Amount is {}{0}' StringFormat='Amount is \{0\}' 

I even tried changing the data type of the binding property to int , string and double . Nothing is working. This is a very common use case, but it does not seem to be supported.

+58
wpf binding wpf-controls wpfdatagrid wpftoolkit
Nov 17 '10 at 16:23
source share
5 answers

The reason this does not work is because the Label.Content property is of type Object , and Binding.StringFormat used only when binding to a property of type String .

What's happening:

  • Binding boxes your MaxLevelOfInvestment value and stores its Label.Content property as a decimal value in the box.
  • The Label control has a template that includes ContentPresenter .
  • Since the ContentTemplate not installed, ContentPresenter searches for the DataTemplate defined for the Decimal type. When it finds nothing, it uses the default template.
  • The default template used by ContentPresenter represents strings using the label property ContentStringFormat .

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's how to use Label.ContentStringFormat:

 <Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}" /> 

Here's how to use a TextBlock:

 <TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" /> 

Note. For simplicity, I skipped one detail in the above explanation: ContentPresenter actually uses its own Template and StringFormat , but at boot time they are automatically bound to a template for the ContentTemplate and ContentStringFormat Label properties, so it seems that ContentPresenter actually uses Label properties.

+156
Nov 17 '10 at 21:54
source share

I just checked, and for some reason, it doesn't work with the shortcut, probably because it uses ContentPresenter for the Content property inside. Instead, you can use TextBlock and it will work. You can also put the TextBlock excerpt below in the label content if you need to inherit style, behavior, etc.

 <TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is \{0\}'} /> 
+3
Nov 17 '10 at 16:28
source share

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 a format string, for example Completed {0} tasks out of {1} .

+3
Mar 01 '16 at 18:03
source share

Try using the 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; } } 
+1
Nov 17 '10 at 16:26
source share

Perhaps this will help ...

Paste the code in XAML

0
Nov 17 '10 at
source share



All Articles