【问题标题】:Setting WPF text to TextBlock将 WPF 文本设置为 TextBlock
【发布时间】:2010-12-12 23:48:07
【问题描述】:

我知道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. 应用程序将用户输入保存为来自RichTextBoxFlowDocument,并将其序列化到磁盘。
  3. FlowDocument 从磁盘反序列化为变量text
  4. 现在,我希望能够在 TextBlock 中显示用户文本。

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


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

【问题讨论】:

    标签: c# wpf xaml textblock flowdocument


    【解决方案1】:

    如下创建并添加对象:

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

    【讨论】:

    • run.Foreground = Brushes.Maroon;
    【解决方案2】:

    我知道TextBlock可以呈现FlowDocument

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

    【讨论】:

      【解决方案3】:

      如果您的 FlowDocument 已被反序列化,这意味着您有一个 FlowDocument 类型的对象,对吧?尝试将 TextBlock 的 Text 属性设置为此值。当然,txtFont.Text = ... 不能这样做,因为这只适用于字符串。对于其他类型的对象,需要直接设置DependencyProperty:

      txtFont.SetValue(TextBlock.TextProperty, myFlowDocument)
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-01
          • 2013-01-15
          • 2017-05-07
          • 2012-10-24
          • 1970-01-01
          • 2011-08-22
          • 1970-01-01
          相关资源
          最近更新 更多