【发布时间】:2019-10-11 11:11:37
【问题描述】:
给定 xaml 代码,如
<RichTextBlock x:Name="richb"> </RichTextBlock>
如何从后面的 c++ 代码向名为richb 的 RichTextBlock 添加文本?
如果它是一个 TextBlock,那就是
richb().Text(L"Any text can go here");
但这不适用于 RichTextBlock。
【问题讨论】:
给定 xaml 代码,如
<RichTextBlock x:Name="richb"> </RichTextBlock>
如何从后面的 c++ 代码向名为richb 的 RichTextBlock 添加文本?
如果它是一个 TextBlock,那就是
richb().Text(L"Any text can go here");
但这不适用于 RichTextBlock。
【问题讨论】:
RichTextBlock 与 TextBlock 不同,您需要使用 Paragraph 元素来定义要在 RichTextBlock 控件中显示的文本块。关于更多信息,可以参考这个document。
#include "winrt/Windows.UI.Xaml.Documents.h"
using namespace winrt;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Documents;
Paragraph paragraph = Paragraph();
Run run = Run();
// Customize some properties on the RichTextBlock.
richb().IsTextSelectionEnabled(true);
richb().TextWrapping(TextWrapping::Wrap);
run.Text(L"This is some sample text to show the wrapping behavior.");
// Add the Run to the Paragraph, the Paragraph to the RichTextBlock.
paragraph.Inlines().Append(run);
richb().Blocks().Append(paragraph);
【讨论】: