resx 文件中用于本地化的混合字体类型



我正在做一些本地化,遇到了一个我似乎无法弄清楚的问题。

我需要显示以下内容:tcpCO₂(其中 tcp 为斜体)

<Italic>tcp</Italic> CO₂

我以为我可以把 HTML 放在我的 .resx 文件中,一切都会结婚,但输出的内容显示了 html,包括括号等。有人对此事有意见吗?

我使用附加行为在TextBlock中显示丰富的 XAML 文本:

public static class TextBlockBehavior
{
    [AttachedPropertyBrowsableForType(typeof(TextBlock))]
    public static string GetRichText(TextBlock textBlock)
    {
        return (string)textBlock.GetValue(RichTextProperty);
    }
    public static void SetRichText(TextBlock textBlock, string value)
    {
        textBlock.SetValue(RichTextProperty, value);
    }
    public static readonly DependencyProperty RichTextProperty =
        DependencyProperty.RegisterAttached(
          "RichText",
          typeof(string),
          typeof(TextBlockBehavior),
          new UIPropertyMetadata(
            null,
            RichTextChanged));
    private static void RichTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        TextBlock textBlock = o as TextBlock;
        if (textBlock == null)
            return;
        var newValue = (string)e.NewValue;
        textBlock.Inlines.Clear();
        if (newValue != null)
            AddRichText(textBlock, newValue);
    }
    private static void AddRichText(TextBlock textBlock, string richText)
    {
        string xaml = string.Format(@"<Span>{0}</Span>", richText);
        ParserContext context = new ParserContext();
        context.XmlnsDictionary.Add(string.Empty, "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        var content = (Span)XamlReader.Parse(xaml, context);
        textBlock.Inlines.Add(content);
    }
}

你可以像这样使用它:

<TextBlock bhv:TextBlockBehavior.RichText="{x:Static Resources:Resource.CO2}">

但是,在您的情况下,无法直接访问TextBlock,因为它位于LayoutPanel的标题模板中。因此,您需要重新定义此模板以将行为应用于TextBlock

<dxdo:LayoutPanel Name="PanelCo2" Caption="{x:Static Resources:Resource.CO2}">
    <dxdo:LayoutPanel.CaptionTemplate>
        <DataTemplate>
            <TextBlock bhv:TextBlockBehavior.RichText="{Binding}">
        </DataTemplate>
    </dxdo:LayoutPanel.CaptionTemplate>
</dxdo:LayoutPanel>

最新更新