是否可以为 RichTextBox 中预先创建的段落赋值
考虑以下 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.
我已经尝试过了,但它不起作用。到目前为止,我读到的示例似乎都是以编程方式创建 FlowDocument
、Paragraph
,然后将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法将 XAML 中声明的段落替换为新段落,但可以直接使用它。
保持 XAML 不变,将代码更改为此,它将起作用:
或者,只需将新段落添加到
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:
Alternatively, just add the new paragraph to the
FlowDocument
, rather than trying to assign it to the existing paragraph.