【问题标题】:Format text from resourceDictionary in xaml格式化xaml中resourceDictionary的文本
【发布时间】:2016-02-04 10:34:44
【问题描述】:

我的资源字典中有一段文字:

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

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

【问题讨论】:

    标签: xaml formatting resourcedictionary


    【解决方案1】:

    字符串本身不支持富文本格式,可以使用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>
    

    【讨论】:

    • 感谢您的回答。但我想知道,是否有动态资源的方法
    • 对不起,如果使用InlineCollections,它似乎必须是StaticResourceDynamicResource要求属性为DependencyProperty
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多