簡體   English   中英

如何加載WPF FormattedText對象(從RTF或RichTextBox)

[英]How to load a WPF FormattedText object (from RTF or RichTextBox)

我的WPF應用程序在一塊大畫布上的不同位置顯示很多文本片段(一種便簽應用程序)

我目前正在使用FormattedText對象渲染文本並將其直接“繪制”到Visual對象中(以提高速度/效率)

我面臨的挑戰是如何加載/保存/編輯富文本格式。 我想使用RichTextBox來編輯文本-但我找不到從文本框中獲取文本並放入FormattedText對象的方法(反之亦然)

有人知道如何實現嗎? 我能想到的唯一方法是在FormattedText對象上具有某種“從RTF串行化/從RTF串行化”的功能-但這似乎並不存在。

謝謝

您可以遍歷RichTextBox.Document內部的所有內聯對象,獲取所有您感興趣的依賴項屬性,然后將它們設置在新的FormattedText對象上。

var formattedTextToDraw = new List<FormattedText>();

foreach (var paragraph in RichTextBox.Document.OfType<Paragraph>())
{
    foreach(var inline in paragraph)
    {
        formattedTextToDraw.Add(new FormattedText(
            inline.Text, //Text
            inline.FontSize, //Fontsize
            inline.Foreground, //Color
            etc....) //Other properties for FormattedText constructor
    }
}

Clemens在http://www.wpfmentor.com/2009/01/how-to-transfer-rich-text-from.html上方發布的鏈接解決了我的問題。

與fooook的答案類似-遍歷內聯對象,並將其屬性應用於FormattedText對象。

可惜的是FormattedText不支持圖像(例如iOS / OSX上的NSAttributedString)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM