Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringFormat in silverlight Xaml and resources

I have format strings in my resource files. I am trying to access these from the Text attribute of TextBlock using FormatString

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

I am getting the following error:

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

Error points to Text=.

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

like image 614
Nasser Avatar asked Oct 26 '10 15:10

Nasser


2 Answers

Binding.StringFormat is not a dependency property and therefore you cannot set a binding to this property. If you want to assign a value to that property, your value has to be a static resource, like this:

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

You should declare your resource like this:

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

The end result looks like this:

Resource String Format

like image 112
Murven Avatar answered Sep 20 '22 00:09

Murven


Your syntax is wrong for using StringFormat and you may want something other than StringFormat. StringFormat is used to manipulate the output of what is assigned to the Path of the Binding. In your example you're binding to the Project.Name property.

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

Other answers around this topic:

Does Silverlight support StringFormat in binding?

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

Here's some example code of using StringFormat:

<TextBlock Text="{Binding Path=Cost, StringFormat=\{0:c\}}" />
like image 31
Joe McBride Avatar answered Sep 22 '22 00:09

Joe McBride