将 WPF 文本设置为 TextBlock

发布于 2024-08-10 20:18:38 字数 1059 浏览 4 评论 0原文

我知道 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 中呈现用户文本。

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


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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺ぐ爱和你が 2024-08-17 20:18:38

创建并添加对象如下:

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

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);
凡间太子 2024-08-17 20:18:38

我知道TextBlock可以呈现FlowDocument

您为什么这么认为?我不认为这是真的... TextBlock 的内容是 Inlines 属性,它是一个 InlineCollection。所以它只能包含 Inline...但是在 FlowDocument 中,内容是 Blocks 属性,其中包含 的实例阻止。并且 Block 不是 Inline

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

聊慰 2024-08-17 20:18:38

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

txtFont.SetValue(TextBlock.TextProperty, myFlowDocument)

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)
缱倦旧时光 2024-08-17 20:18:38

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文