3

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

0

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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)

2 Comments

ok, this confirms what I wrote, i.e.: the xamlParsing cannot create the FlowDocument. Question is: why? I'll have a look and try to reproduce this to get an idea.
actually, the more I look at this, the less I understand what you're doing :p ... what do you set as CMSText? do you explicitly set this to the resource you mention earlier? I don't understand the link between your binding and the flowDoc resource mentioned above. because the problem is more than probably that the "value" in your converter is not set properly (the converter itself does not seem broken, neither does the xamlParser, so I assume it's the value that isn't good). Can you check what the "value" variable contains when you go through the converter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.