2011-06-27 6 views
0

私はWPF RichTextBoxを使用しています。 私は実行時に書式設定を行います。通常正常に動作します。しかし、データベースからリロードした後、正しく動作しません。 これは私が使用しているサンプルコードです。wpf:リッチテキストボックスランタイム、データベースから戻ってくるときにWokingしない

var selection = TxtRtf1.Selection; 
if (!selection.IsEmpty) 
{ 
    var tdc =(TextDecorationCollection) selection.GetPropertyValue(Inline.TextDecorationsProperty); 
    if (tdc == null || !tdc.Equals(TextDecorations.Underline)) 
     selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline); 
    else 
     selection.ApplyPropertyValue(Inline.TextDecorationsProperty, null); 
} 

< tdc.Equals(TextDecorations.Underline)>は常にfalseを返すが起こる実際のもの。データベースからデータを読み込んだ後

答えて

0

Try tdc.SequenceEqual(TextDecorations.Underline)

var tdc =(TextDecorationCollection) selection.GetPropertyValue(Inline.TextDecorationsProperty); 
if (tdc == null || !tdc.SequenceEqual(TextDecorations.Underline)) 
    selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline); 
else 
    selection.ApplyPropertyValue(Inline.TextDecorationsProperty, null); 
+0

リッチテキストボックスを下線付きのテキストでリロードすると機能しません –

0

解決策はhereが私のために働くようです。私はこれをやや修正しました。もちろん、ブール情報をユーザーに表示するには、データバインディングやその他の方法を使用する必要があります。

private bool RTEHasUnderlinedText() 
    { 
     var sel = richTextBox.Selection; 
     var value = GetPropertyValue(sel, Paragraph.TextDecorationsProperty); 
     return ((TextDecorationCollection)(value)).Count > 0; 
    } 

    private Object GetPropertyValue(TextRange textRange, DependencyProperty formattingProperty) 
    { 
     Object value = null; 
     var pointer = textRange.Start; 
     if (pointer is TextPointer) 
     { 
      Boolean needsContinue = true; 
      DependencyObject element = ((TextPointer)pointer).Parent as TextElement; 
      while (needsContinue && (element is Inline || element is Paragraph || element is TextBlock)) 
      { 
       value = element.GetValue(formattingProperty); 
       IEnumerable seq = value as IEnumerable; 
       needsContinue = (seq == null) ? value == null : seq.Cast<Object>().Count() == 0; 
       element = element is TextElement ? ((TextElement)element).Parent : null; 
      } 
     } 
     return value; 
    } 
関連する問題