从文件读取时,C#WPF RichText Box Background Property返回null



我正在创建RichText编辑器,保存/加载文件等。

我可以使用:

为文本(或背景颜色(分配突出显示颜色
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);

可以在WordPad上打开此文件,一切正常

现在,我可以再次将文件加载到我的程序中:

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();

,它使用文本亮点,字体,大小,一切正确显示,就像wordpad

一样正确

这是一个问题:

但是当我运行

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

即使高光正在工作(可见(

,它也会返回空

很奇怪的是,当我从放置中分配时,它确实会返回正确的颜色,然后要求它,但是加载文件时,就像the Textrange是否没有背景property,但其他所有属性都在那里,并且甚至怪异的是,在调试器上,我可以分析SelectionTextrange变量,它具有一个具有正确背景的" XML",只是可以从任何形式访问此" XML",从debugger可见

我找到了其他两个类似的问题,但没有答案:

(C#WPF(如何更改Textrange背景颜色?

https://webcache.googleusercontent.com/search?q=cache:ipukfyskonqj:https://stackoverflow.com/questions/questions/questions/444595989/wpf-not-not-not-not-not-not-not-not-remembern--remembering-background-background-bacgorgn-on-load-from-load-from-a-a-save-save- & cd = 1& hl = es& ct = clnk&amp& 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可以以某种方式进行优化,但这可以正常工作。

最新更新