在xaml中格式化来自resourceDictionary的文本

时间:2016-02-04 10:34:44

标签: xaml formatting resourcedictionary

我的资源词典中有一个文本:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="ResourceDictionaryName" Localization.Comments="$Content(DoNotLocalize)">Loc-en-EN</system:String>

<!--MainControl.xaml-->   
<system:String x:Key="PersonalEmail">Please enter your email./system:String></ResourceDictionary>

我以这种方式将它绑定到xaml:

   <TextBlock Text="{DynamicResource PersonalEmail}" Style="{DynamicResource TextBlockStyle}"/>

是否可以创建样式或转换器,例如,粗体显示,其余文本正常显示?

1 个答案:

答案 0 :(得分:0)

字符串本身不支持富文本格式,您可以使用Span来显示文本。

<TextBlock>
    <Span>
        <Bold>Please</Bold> enter your email.
    </Span>
</TextBlock>

你可以将几乎任何内容放入ResourceDictionary并给它一个密钥,并使用该密钥引用它。

<Window.Resources>
    <Span x:Key="PersonalEmail">
        <Bold>Please</Bold> enter your email.
    </Span>
</Window.Resources>
<Grid>
    <TextBlock>            
        <StaticResource ResourceKey="PersonalEmail" />
    </TextBlock>
</Grid>