将WPF文本设置为TextBlock

时间:2009-11-04 11:24:58

标签: c# wpf xaml textblock flowdocument

我知道TextBlock可以提供FlowDocument,例如:

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

我想知道如何将存储在变量中的FlowDocument设置为TextBlock。 我正在寻找类似的东西:

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

但是,上面代码的结果是XAML文本显示为未解析。


编辑:我想我的问题不够明确。我真正想要实现的目标是:

  1. 用户将一些文本输入 RichTextBox
  2. 应用程序将用户输入保存为 RichTextBox 中的FlowDocument,并将其序列化为磁盘。
  3. FlowDocument从磁盘反序列化为变量 text
  4. 现在,我希望能够在TextBlock
  5. 中展示用户文字

    因此,据我所知,创建一个新的运行对象并手动设置参数并不能解决我的问题。


    问题是序列化 RichTextBox 会创建 Section 对象,我无法将其添加到 TextBlock.Inlines 。因此,无法将反序列化的对象设置为 TextBlock TextProperty

4 个答案:

答案 0 :(得分:5)

创建并添加对象,如下所示:

        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);

答案 1 :(得分:3)

  

我知道TextBlock可以呈现FlowDocument

是什么让你这么想?我不认为这是真的...... TextBlock的内容是Inlines属性,即InlineCollection。因此它只能包含Inline ...但在FlowDocument中,内容是Blocks属性,其中包含Block的实例。而Block不是Inline

答案 2 :(得分:0)

如果您的 FlowDocument 已经反序列化,则表示您拥有FlowDocument类型的对象,对吧?尝试将TextBlock的Text属性设置为此值。当然,您不能使用txtFont.Text = ...执行此操作,因为这仅适用于字符串。对于其他类型的对象,您需要直接设置DependencyProperty:

txtFont.SetValue(TextBlock.TextProperty, myFlowDocument)

答案 3 :(得分:0)

以下是我们如何通过动态分配样式来设置文本块的外观。

    // 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;

您可以直接删除样式部分并设置属性,但我们喜欢将样式捆绑在样式中以帮助我们整理项目中的外观。

textBlock_DealerMessage.FontWeight = thisWeight;

HTH。