在DynamicResource中添加空间?
问题描述:
我需要的文本前添加一个空格,我的结构是这样的:在DynamicResource中添加空间?
<Setter Property="Text" Value="{DynamicResource oneMatch}"/>
所以显示的内容应该是:" oneMatchContent"
在过去,我也使用了类似的事情StringFormat
:
<TextBlock Text="{Binding oneMatch, StringFormat=This is {0}}"/>
但我没有看到任何想法?Value
的StringFormat
任何想法?
答
我无法确定,因为您对所有这些背景都非常隐秘,但我最好的猜测是Setter
在Style
中,您正在申请TextBlock
。
如果是这种情况,您可以使用Label
替代(或其他ContentControl
的后代),并设置其ContentStringFormat
属性。
<Style
x:Key="oneMatchLabelStyle"
TargetType="Label"
BasedOn="{StaticResource {x:Type Label}}"
>
<Setter Property="Content" Value="{DynamicResource oneMatch}" />
<Setter Property="ContentStringFormat" Value="This is {0}" />
<!-- Set padding to 0 so it'll look like TextBlock did in your layout -->
<Setter Property="Padding" Value="0" />
</Style>
...
<Label Style="{DynamicResource oneMatchLabelStyle}" />
如果你想添加的字符串资源本身领先的空间,只需指定在XAML非打破空间(Unicode U+00A0
)。该HTML字符实体
不XAML支持,所以使用十六进制字符实体,而不是:
<sys:String x:Key="oneMatch"> Blah blah blah</sys:String>
,然后使用该资源没有任何特殊格式。
+0
为解决问题,而不是在'
答
你可以看上这种方法:
<Window.Resources>
<sys:String x:Key="SecretKey">SecretText</sys:String>
<Style x:Key="ContentKey" TargetType="Label">
<Setter Property="Content">
<Setter.Value>
<TextBlock>
<TextBlock.Inlines>
<Run Text=" "/>
<Run Text="{DynamicResource SecretKey}"/>
</TextBlock.Inlines>
</TextBlock>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
...
<Label Style="{StaticResource ContentKey}"/>
可以在Run
使用任何文本了。
如何应用转换器? http://stackoverflow.com/questions/378979/is-it-possible-to-use-a-converter-within-a-style – zquanghoangz