是否可以为 RichTextBox 中预先创建的段落赋值

发布于 2024-12-05 07:24:12 字数 937 浏览 0 评论 0原文

考虑以下 XAML 代码:

<RichTextBox Name="dataRichTextBox" VerticalScrollBarVisibility="Auto" >
    <FlowDocument Name="dataFlowDocument" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Paragraph Name="dataParagraph">

        </Paragraph>
    </FlowDocument>
</RichTextBox>

我想要做的是,直接将以编程方式创建的 Paragraph 分配给 dataParagraph,如 <强>XAML。

其代码看起来像这样:

Paragraph paraOne = new Paragraph();
Run run1 = new Run("I am run one"+Environment.NewLine);
// run1.Background = Brushes.Green;
paraOne.Inlines.Add(run1);
dataParagraph = paraOne; // expect that it will show up on the RichTextBox.

我已经尝试过了,但它不起作用。到目前为止,我读到的示例似乎都是以编程方式创建 FlowDocumentParagraph,然后将 Runs 分配给它们。是否可以按照我实现的方式实现它。

Consider the following XAML code:

<RichTextBox Name="dataRichTextBox" VerticalScrollBarVisibility="Auto" >
    <FlowDocument Name="dataFlowDocument" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Paragraph Name="dataParagraph">

        </Paragraph>
    </FlowDocument>
</RichTextBox>

What I would like to do is, directly assign a a Paragraph, created programmatically, to dataParagraph as defined in the XAML.

the code for that looks something like:

Paragraph paraOne = new Paragraph();
Run run1 = new Run("I am run one"+Environment.NewLine);
// run1.Background = Brushes.Green;
paraOne.Inlines.Add(run1);
dataParagraph = paraOne; // expect that it will show up on the RichTextBox.

I have tried it, and it doesn't work. The examples I read so far, all seem to create the FlowDocument, Paragraph Programmatically and then assign Runs' to them. Is it possible to achieve it the way I have implemented.

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

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

发布评论

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

评论(1

一袭白衣梦中忆 2024-12-12 07:24:12

您无法将 XAML 中声明的段落替换为新段落,但可以直接使用它。

保持 XAML 不变,将代码更改为此,它将起作用:

dataParagraph.Inlines.Add(new Run("I am run one" + Environment.NewLine));

或者,只需将新段落添加到 FlowDocument 中,而不是尝试将其分配给现有段落。

You can't replace the paragraph declared in XAML with a new one, but you can work with it directly.

Keeping your XAML as-is, change your code to this, and it will work:

dataParagraph.Inlines.Add(new Run("I am run one" + Environment.NewLine));

Alternatively, just add the new paragraph to the FlowDocument, rather than trying to assign it to the existing paragraph.

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