给出xaml代码
<RichTextBlock x:Name="richb"> </RichTextBlock>
如何从后面的C ++代码向名为richb的RichTextBlock添加文本?
如果它是一个TextBlock,那就应该是
richb().Text(L"Any text can go here");
但是,这不适用于RichTextBlock。
答案 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);