Search code examples
xamlformattingresourcedictionary

Format text from resourceDictionary in xaml


I have a text in my resource dictionary:

<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>

and I bind it to xaml this way:

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

Is it possible to create style or converter, to show, for example, Please bold, and rest of the text as normal?


Solution

  • String itself does not support rich text format, you can use a Span to show the text.

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

    And you can put almost anything into a ResourceDictionary and give it a key, and reference it using that key.

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