2017-06-29 72 views
2

我創建一個富文本編輯器讀取時返回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://stackoverflow.com/questions/44595989/wpf-not-remembering-backgroundproperty-on-load-from-a-save+&cd=1&hl=es&ct=clnk&gl=co

回答

1

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能以某種方式進行優化,但是這是工作。