【问题标题】:How Can I add Text to a RichTextBlock from the code behind in a c++ winrt UWP app,如何从 c++ winrt UWP 应用程序中的代码将文本添加到 RichTextBlock,
【发布时间】: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。

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

    • ``` #include ``` 部分特别有用。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多