I am doing some localization and have bumped into a problem that I cant seem to figure out.
I need the following displayed: tcpCO₂ (where tcp is in italic)
<Italic>tcp</Italic> CO₂
I thought I could just put the HTML in my .resx file and all would be marry, but the content of the output shows the html including brackets etc. Does anyone have inputs in this matter?
I use an attached behavior to display rich XAML text in a TextBlock
:
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);
}
}
You can use it like this:
<TextBlock bhv:TextBlockBehavior.RichText="{x:Static Resources:Resource.CO2}">
However, in your case don't have access to the TextBlock
directly, since it's in the caption template of the LayoutPanel
. So you need to redefine this template to apply the behavior on the 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>