I am trying to externalise some of the wording within my WPF application, however I would like to be able to use some degree of formatting as well.
My initial thought was to use a string resource which represented a FlowDocument or Paragraph such as:
<FlowDocument>
<Paragraph FontSize="16" Foreground="Blue">Some display text under content management</Paragraph>
</FlowDocument>
In the UI I have been trying to bind this using a IValueConverter:
<ContentControl Content="{Binding Path=CMSText,Source={StaticResource Resources},Converter={StaticResource flowDocConverter}"/>
In the converter:
StringReader sr = new StringReader(value.ToString());
XamlReader xamlReader = XamlReader.Create(sr);
return (FlowDocument)xamlReader.Parse();
but it keeps throwing an exception on the return statement.
Is it even possible to do this via a binding?
And where am I going wrong in the XamlReader?
XamlParseException
'Cannot create unknown type 'FlowDocument'.' Line number '1' and line position '2'.
Change your input string FlowDocument Tag, adding the namespace in the xmlns attribute like so:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:MARS">
<Paragraph FontSize="16" Foreground="Blue">Some display text under content management</Paragraph>
</FlowDocument>
I'd say you simply cannot cast the result of xamlReader.Parse() into a FlowDocument (I'm not sure why).
you should rather try something like this as your converter:
FlowDocument myFlowDoc = new FlowDocument();
myFlowDoc.Blocks.Add(new Paragraph(new Run(value)))
return myFlowDoc;
(I find FlowDocument management lacks simplicity and tends to be a hassle)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With