views:

29

answers:

2

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"?

+1  A: 

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:

http://stackoverflow.com/questions/398353/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\}}" />
Joe McBride
The content of Binding Path=WkStrings.DisplayProjectName, Source={StaticResource ResourceWrapper}} is actually a format specifier (something like "Project: {0}"). What is odd is that I can read the string out of the resx file, but not use its content as format specifier for the function FormatString. Where else can I store my format specifiers (while having them available globally)?
Nasser
Ah, I see what you're trying to do now. I'm not sure that's supported. Right now I would probably suggest exposing the formatted text as a property of a ViewModel. Or using a multi binding and a converter to piece the two together. See this post: http://www.scottlogic.co.uk/blog/colin/2009/06/silverlight-multibindings-how-to-attached-mutiple-bindings-to-a-single-property/
Joe McBride
+1  A: 

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

Murven
I need to keep the format-specifiers at a location that makes them accessible from the entire application and *"localizable"*. How would I go about achieving this? The only solution I can think of at the moment is a wrapper around the resource managers (something like a locator) which I think is a bit of an overkill.
Nasser
What we ended up with was making them available through the ViewModel system. We have an aggregator for this kind of formatters as a property of the base view model for the project and so Blend users have them all available right way - o every view - as all view models derive from this base. When new formatters are added to the strings library a wrapper property is added to the aggregator class and that makes it available application-wide. It is a bit on an overkill, but it works.
Murven