【问题标题】:Styling text part in flow document在流文档中设置文本部分的样式
【发布时间】:2015-02-22 19:23:13
【问题描述】:

我在我的 XAML 文件中定义了以下内容:

<FlowDocumentScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
    <FlowDocument Name="FlowDocument1" PagePadding="10" FontFamily="Segoe UI" FontSize="22">
        <Paragraph>
            Those who have denied the reality of moral distinctions, may be
            ranked among the disingenuous disputants; nor is it conceivable,
            that any human creature could ever seriously believe, that all
            characters and actions were alike entitled to the affection and
            regard of everyone. The difference, which nature has placed
            between one man and another, is so wide, and this difference is
            still so much farther widened, by education, example, and habit,
            that, where the opposite extremes come at once under our
            apprehension, there is no scepticism so scrupulous, and scarce
            any assurance so determined, as absolutely to deny all
            distinction between them. Let a man's insensibility be ever so
            great, he must often be touched with the images of Right and
            Wrong; and let his prejudices be ever so obstinate, he must
            observe, that others are susceptible of like impressions. The
            only way, therefore, of converting an antagonist of this kind, is
            to leave him to himself.
        </Paragraph>
    </FlowDocument>
</FlowDocumentScrollViewer>

如何在 C# 中,对于本段中定义的某些文本部分,更改背景颜色或使其变为粗体或斜体?

【问题讨论】:

  • 每次都会是文档的相同部分吗?例如,“绝对否认它们之间的所有区别”总是斜体,还是会从正常变为斜体?或者您需要在任意时间更改文档中的任意单词?
  • &lt;Run&gt; 你在找什么吗?
  • 嗯,我希望能够一直修改它。这意味着,我可能会更改段落内容,或段落中的特定部分......
  • @paul.abbott.wa.us 我以前从未使用过 FlowDocumentScrollViewer,也从未使用过 Run 类。我只想看一个有关如何访问段落并更改其内容的示例。例如:更改文本的特定部分,将其更改为粗体、斜体、突出显示。我是否应该在 FlowDocumentScrollViewer 中也为我的段落组件定义名称,或者可以说类似 fd_Component.Document.Paragraph ...?

标签: c# wpf flowdocument paragraph


【解决方案1】:

如果您熟悉 Nodes,那么简短的回答就是将您的代码视为 xml 文件。流文档由块组成,有段落、行等。

http://www.codeproject.com/Articles/37368/WPF-Flow-Document-For-Beginners

例如。你的 FlowDocument 没有名字,所以我们称之为 flowDocument1,所以

flowDocument1.Blocks.Paragraph.Text = "Here"

【讨论】:

  • 好的,我现在将在这段代码中为我的 FlowDocument 设置名称。但是,让我们回到这个问题,以便我可以接受您的回答。我如何才能将本段的特定部分文本更改为其他文本,或使其变为粗体或斜体?
  • 您必须使用 find 或正则表达式来查找您想要的特定文本并将其替换为您的新文本
猜你喜欢
  • 1970-01-01
  • 2015-03-18
  • 2019-10-31
  • 2012-11-08
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 2023-03-22
相关资源
最近更新 更多