Silverlight Xaml和资源中的StringFormat

Nas*_*ser 6 xaml binding silverlight-4.0

我的资源文件中有格式字符串.我试图使用FormatString从TextBlock的Text属性访问这些

Text="{Binding Path=Project.Name, StringFormat={Binding Path=WkStrings.DisplayProjectName, Source={StaticResource ResourceWrapper}}}"
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Provide value on 'System.Windows.Data.Binding' threw an exception
Run Code Online (Sandbox Code Playgroud)

错误指向Text =.

是否可以从"嵌套绑定"访问资源?

Mur*_*ven 8

Binding.StringFormat不是依赖项属性,因此您无法设置对此属性的绑定.如果要为该属性赋值,则您的值必须是静态资源,如下所示:

<TextBlock Text="{Binding ProjectName, StringFormat={StaticResource ProjectNameFormat}}"/>
Run Code Online (Sandbox Code Playgroud)

您应该声明您的资源:

<UserControl.Resources>
    <System:String x:Key="ProjectNameFormat">Project: {0}</System:String>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

最终结果如下:

资源字符串格式