Reputation: 3545
I'd like to write some text in a RichTextBox from the WPF toolkit with the degree (°) sign.
I simply tried
Section section = new Section();
Paragraph paragraph = new Paragraph();
section.Blocks.Add(paragraph);
string str = string.Format("Temperature : {0:0.00}°C", temp);
text = new Run(str);
paragraph.Inlines.Add(text);
TemperatureText = System.Windows.Markup.XamlWriter.Save(section);
But the degree sign is replaced with a "?". I also tried to directly write the unicode string.Format("Temperature : {0:0.00}\u00B0C", temp)
but it also failed.
Do you have any idea ? Thanks !
[EDIT]
I'm using the XamlFormatter for the RichTextBox
Upvotes: 2
Views: 2044
Reputation: 946
Try implementing your own formatter, that acts like the XAMLFormatter but using UTF8 Encoding:
public class MyXamlFormatter : ITextFormatter
{
public string GetText( System.Windows.Documents.FlowDocument document )
{
TextRange tr = new TextRange( document.ContentStart, document.ContentEnd );
using( MemoryStream ms = new MemoryStream() )
{
tr.Save( ms, DataFormats.Xaml );
return Encoding.UTF8.GetString(ms.ToArray());
}
}
public void SetText( System.Windows.Documents.FlowDocument document, string text )
{
try
{
if( String.IsNullOrEmpty( text ) )
{
document.Blocks.Clear();
}
else
{
TextRange tr = new TextRange( document.ContentStart, document.ContentEnd );
using( MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
{
tr.Load( ms, DataFormats.Xaml );
}
}
}
catch
{
throw new InvalidDataException( "Data provided is not in the correct Xaml format." );
}
}
}
So, in your XAML:
<wtk:RichTextBox.TextFormatter>
<myNameSpace:MyXamlFormatter/>
</wtk:RichTextBox.TextFormatter>
It should works.
Upvotes: 1