如何在c ++ winrt UWP应用中从背后的代码向RichTextBlock添加文本,

时间:2019-10-11 02:00:16

标签: c++ xaml uwp c++-winrt

给出xaml代码


<RichTextBlock x:Name="richb"> </RichTextBlock>

如何从后面的C ++代码向名为richb的RichTextBlock添加文本?​​

如果它是一个TextBlock,那就应该是

richb().Text(L"Any text can go here");

但是,这不适用于RichTextBlock。

1 个答案:

答案 0 :(得分:1)

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