Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlowDocument sourced from external resource throws a XamlParseException

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

like image 536
benPearce Avatar asked Aug 27 '26 01:08

benPearce


2 Answers

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>
like image 66
Louis Somers Avatar answered Aug 29 '26 13:08

Louis Somers


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)

like image 40
David Avatar answered Aug 29 '26 13:08

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!