StringFormat in Silverlight Xaml and Resources

I have formatting strings in resource files. I am trying to access them from the Text TextBlock attribute using FormatString

Text="{Binding Path=Project.Name, StringFormat={Binding Path=WkStrings.DisplayProjectName, Source={StaticResource ResourceWrapper}}}" 

I get the following error:

 Provide value on 'System.Windows.Data.Binding' threw an exception 

The error indicates the text =.

Is it possible to access resources from a "nested binding"?

+6
binding xaml
source share
2 answers

Binding.StringFormat is not a dependency property and therefore you cannot bind to this property. If you want to assign a value to this property, your value must be a static resource, for example:

 <TextBlock Text="{Binding ProjectName, StringFormat={StaticResource ProjectNameFormat}}"/> 

You must declare your resource as follows:

 <UserControl.Resources> <System:String x:Key="ProjectNameFormat">Project: {0}</System:String> </UserControl.Resources> 

The end result is as follows:

Resource String Format

+8
source share

Your syntax is incorrect for using StringFormat, and you might want something other than StringFormat. StringFormat is used to control the output of what is assigned to the Binding Path. In your example, you are bound to the Project.Name property.

StringFormat should be used to achieve a similar effect using String.Format in the code. See this link for formatting: http://msdn.microsoft.com/en-us/library/26etazsy(v=VS.95).aspx

Other answers on this topic:

Does Silverlight support StringFormat binding?

http://blog.davemdavis.net/2009/12/03/silverlight-4-data-binding-string-format/

Here is sample code using StringFormat:

 <TextBlock Text="{Binding Path=Cost, StringFormat=\{0:c\}}" /> 
+1
source share

All Articles