Setting WPF text to TextBlock
Asked Answered
R

4

7

I know that TextBlock can present a 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 a 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.

Ruhnke answered 4/11, 2009 at 11:24 Comment(0)
H
3

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

Hone answered 4/11, 2009 at 13:12 Comment(0)
C
5

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);
Cram answered 4/11, 2009 at 11:37 Comment(1)
run.Foreground = Brushes.Maroon;Mazdaism
H
3

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

Hone answered 4/11, 2009 at 13:12 Comment(0)
C
0

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)
Chafee answered 4/11, 2009 at 12:39 Comment(0)
F
0

Here is how we are setting the look of a textblock by assigning a style on-the-fly.

    // Set Weight (Property setting is a string like "Bold")
    FontWeight thisWeight = (FontWeight)new FontWeightConverter().ConvertFromString(Properties.Settings.Default.DealerMessageFontWeightValue);

    // Set Color (Property setting is a string like "Red" or "Black")
    SolidColorBrush thisColor = (SolidColorBrush)new BrushConverter().ConvertFromString(Properties.Settings.Default.DealerMessageFontColorValue);

    // Set the style for the dealer message
    // Font Family Property setting  is a string like "Arial"
    // Font Size Property setting is an int like 12, a double would also work
    Style newStyle = new Style
    {
        TargetType = typeof(TextBlock),
        Setters = {
            new Setter 
            {
                Property = Control.FontFamilyProperty,
                Value = new FontFamily(Properties.Settings.Default.DealerMessageFontValue)
            },
            new Setter
            {
                Property = Control.FontSizeProperty,
                Value = Properties.Settings.Default.DealerMessageFontSizeValue
            },
            new Setter
            {
                Property = Control.FontWeightProperty,
                Value = thisWeight
            },
            new Setter
            {
                Property = Control.ForegroundProperty,
                Value = thisColor
            }
        }
    };

    textBlock_DealerMessage.Style = newStyle;

You can eliminate the style section and set properties directly, but we like keeping things bundled in the style to help us organize the look throughout the project.

textBlock_DealerMessage.FontWeight = thisWeight;

HTH.

Fimbriate answered 2/5, 2011 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.