3
votes

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?

EDIT

XamlParseException
'Cannot create unknown type 'FlowDocument'.' Line number '1' and line position '2'.

2
what is the text of the exception you get?David

2 Answers

2
votes

Change your input string FlowDocument Tag, adding the NamePpace 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>
1
votes

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)