tags:

views:

181

answers:

3

I know that TextBlock can present FlowDocument, for example:

<TextBlock Name="txtFont">
     <Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
</TextBlock>

I would like to know how to set FlowDocument that is stored in a variable to a TextBlock. I am looking for something like:

string text = "<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>"
txtFont.Text = text;

However, The result of the code above is that the XAML text is presented unparsed.


EDIT: I guess my question was not clear enough. What I'm really trying to achive is:

  1. The user input some text into a RichTextBox.
  2. The application saves the user input as FlowDocument from the RichTextBox, and serializes it to the disk.
  3. The FlowDocument is deserialized from the disk to the variable text.
  4. Now, I would like to be able to present the user text in a TextBlock.

Therefore, as far as I understand, creating a new Run object and setting the parameters manually will not solve my problem.


The problem is that serializing RichTextBox creates Section object, which I cannot add to TextBlock.Inlines. Therefore, it is not possible to set the deserialized object to TextProperty of TextBlock.

+3  A: 

Hi there,

create and add the object as below:

        Run run = new Run("Courier New 24");
        run.Foreground = new SolidColorBrush(Colors.Maroon);
        run.FontFamily = new FontFamily("Courier New");
        run.FontSize = 24;
        txtFont.Inlines.Add(run);
Blounty
run.Foreground = Brushes.Maroon;
CannibalSmith
true Cannibal. Thanks. :)
Blounty
Thank you for the solution. Please see my edit.
Elad
A: 

If your FlowDocument has been deserialized, it means that you have an object of type FlowDocument, right? Try setting the Text property of your TextBlock to this value. Of course, you cannot do this with txtFont.Text = ..., since this only works for strings. For other types of objects, you need to set the DependencyProperty directly:

txtFont.SetValue(TextBlock.TextProperty, myFlowDocument)
Heinzi
+1  A: 

I know that TextBlock can present FlowDocument

What makes you think that ? I don't think it's true... The content of a TextBlock is the Inlines property, which is an InlineCollection. So it can only contain Inlines... But in a FlowDocument, the content is the Blocks property, which contains instances of Block. And a Block is not an Inline

Thomas Levesque