我創建一個富文本編輯器讀取時返回null,使用保存/載入文件等C#WPF富文本框BackgroundProperty從文件
我可以指定一個突出顯示的顏色的文本(或背景色):
TextRange selectionTextRange = new TextRange(rtb.Selection.Start, rtb.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.BackgroundProperty, backgroundColor);
背景顏色是刷
這部分作品,我可以用它保存到一個文件:
filepath = savedialog.FileName;
TextRange t = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
FileStream file = new FileStream(filepath, FileMode.Create);
t.Save(file, System.Windows.DataFormats.Rtf);
這個文件可以在寫字板打開,一切工作正常
我現在可以加載該文件到我的計劃再次:
range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
fStream = new FileStream(fileName, FileMode.OpenOrCreate);
if (fileName.Substring(fileName.Length - 3).ToLower().Equals("rtf"))
{
range.Load(fStream, DataFormats.Rtf);
}
else
{
range.Load(fStream, DataFormats.Text);
}
fStream.Close();
而且它與文本高亮顯示,字體,大小,一切都正確正確顯示就像在寫字板
這裏談到的問題:
但是當我運行
TextRange selectionTextRange = new TextRange(rtb.Selection.Start, rtb.Selection.End);
SolidColorBrush newBrush = (SolidColorBrush)selectionTextRange.GetPropertyValue(TextElement.BackgroundProperty);
它返回即使亮點是工作空(可見)
怪異的一部分是,它的回報,當我爲它分配從aplication中正確的顏色,然後問它,但加載文件時就好像TextRange沒有BackgroundProperty,但其他所有屬性都存在,而且即使是調試器上的weirder,我也可以分析其selectionTextRange變量,並且它有一個帶有正確背景的「XML」,只是這個「XML」是不能以任何形式進行訪問,從調試器中可以看到
我發現了其他2個類似的問題,但沒有回答:
(C# WPF) How to change textrange background color?