2016-09-27 31 views
0

我需要的文本前添加一个空格,我的结构是这样的:在DynamicResource中添加空间?

<Setter Property="Text" Value="{DynamicResource oneMatch}"/> 

所以显示的内容应该是:" oneMatchContent"

在过去,我也使用了类似的事情StringFormat

<TextBlock Text="{Binding oneMatch, StringFormat=This is {0}}"/> 

但我没有看到任何想法?ValueStringFormat任何想法?

+0

如何应用转换器? http://stackoverflow.com/questions/378979/is-it-possible-to-use-a-converter-within-a-style – zquanghoangz

回答

1

我无法确定,因为您对所有这些背景都非常隐秘,但我最好的猜测是SetterStyle中,您正在申请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字符实体&nbsp;不XAML支持,所以使用十六进制字符实体,而不是:

<sys:String x:Key="oneMatch">&#xa0;Blah blah blah</sys:String> 

,然后使用该资源没有任何特殊格式。

+0

为解决问题,而不是在''是可能的添加一些空格在HTML?什么东西? – Unchained

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="&#xa0;"/> 
         <Run Text="{DynamicResource SecretKey}"/> 
        </TextBlock.Inlines> 
       </TextBlock> 
      </Setter.Value> 
     </Setter> 
    </Style>    
</Window.Resources> 
... 
<Label Style="{StaticResource ContentKey}"/> 

可以在Run使用任何文本了。