开发者

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="{Bind开发者_运维百科ing 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"?


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:

StringFormat in silverlight Xaml and resources


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\}}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜