C#WPF富文本框BackgroundProperty从文件

问题描述:

我创建一个富文本编辑器读取时返回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?

https://webcache.googleusercontent.com/search?q=cache:iPuKFYskoNQJ:https://*.com/questions/44595989/wpf-not-remembering-backgroundproperty-on-load-from-a-save+&cd=1&hl=es&ct=clnk&gl=co

Finaly发现了什么是使用XAML,而不是RTF的问题,我可以“轻松地”阅读为txt和理解发生了什么事(继续使用RTF,只是用XAML我可以明白发生了什么事),代码:

TextRange selectionTextRange = new TextRange(rtb.Selection.Start, rtb.Selection.End); 
SolidColorBrush newBrush = (SolidColorBrush)selectionTextRange.GetPropertyValue(TextElement.BackgroundProperty); 

只读取<Run>属性,如<Run Background="#FF00FF">Some Text</Run>不是<Span><Paragraph>属性,如:

<Paragraph Background="#00FF00"><Span><Run>Some Text</Run></Span></Paragraph> 
<Paragraph Background="#00FF00"><Run>Some Text</Run></Paragraph> 
<Span Background="#00FF00"><Run>Some Text</Run></Span> 
<Paragraph><Span Background="#00FF00"><Run>Some Text</Run></Span></Paragraph> 

或类似的任何其他东西,所以尝试一点我想出了这个和它的工作:

TextRange selectionTextRange = new TextRange(rtb.Selection.Start, rtb.Selection.End); 
SolidColorBrush newBrush = null; 
newBrush = (SolidColorBrush)selectionTextRange.GetPropertyValue(TextElement.BackgroundProperty); 
SolidColorBrush newBrush2 = null; 
newBrush2 = (SolidColorBrush)rtb.Selection.Start.Paragraph.Background; 
SolidColorBrush newBrush3 = null; 
try { 
    newBrush3 = (SolidColorBrush)((Span)((Run)rtb.Selection.Start.Parent).Parent).Background; 
} 
catch (Exception ex) { 
    //Selection Parent is Run 
    //Run Parent can be Span, Paragraph, etc. 
    //Parent is not Span 
} 

if (newBrush == null) { 
    if (newBrush2 == null) { 
     if (newBrush3 == null) { 
      ClrPcker_Bg.SelectedColor = Colors.Transparent; 
      } 
      else { 
       ClrPcker_Bg.SelectedColor = newBrush3.Color; 
      } 
     } 
     else { 
      ClrPcker_Bg.SelectedColor = newBrush2.Color; 
     } 
    } 
    else { 
     ClrPcker_Bg.SelectedColor = newBrush.Color; 
    } 

我觉得最后if能以某种方式进行优化,但是这是工作。