2012-07-26 33 views
0

我必須檢索WPF RichtTextBox中所選文本的Foreground屬性,但我認爲TextRange.GetPropertyValue函數中存在一個錯誤。 我寫了一個簡單的應用程序來測試錯誤:WPF錯誤:RichTextBox中的TextRange.GetPropertyValue

<RichTextBox x:Name="rtfBox"> 
     <RichTextBox.Document> 
      <FlowDocument> 
       <Paragraph> 
        <Run>run run run</Run> 
        <Hyperlink TargetName="http://stackoverflow.com">stackoverflow.com</Hyperlink> 
       </Paragraph> 
      </FlowDocument> 
     </RichTextBox.Document> 
    </RichTextBox> 
    <Button Content="Click!" Height="28" Click="Button_Click" /> 

而且在後面的代碼:

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     var textrange = new TextRange(rtfBox.Selection.Start, rtfBox.Selection.End); 
     var propertyValue = textrange.GetPropertyValue(TextElement.ForegroundProperty); 
     MessageBox.Show(propertyValue.ToString()); 
    } 

當我選擇計算器超級鏈接的前幾個字符,在MessageBox說,前景屬性是一個DependencyProperty.UnsetValue,但是當我選擇鏈接的其他部分時,它顯示真正的前景色。

它看起來像一個錯誤。

有沒有解決這個問題的方法?

回答

0

這是因爲TextElement.ForegroundProperty。 如果將其更改爲Hyperlink.ForegroundProperty,則會顯示真值。

+0

是的,我已經嘗試過,但它不適合我。一切都是一樣的。 – Zsolt 2012-07-26 12:00:51

+0

只有從兩者中選擇一個零件,它纔會起作用。 (例如「run run stackoverfl」)。 – laszlokiss88 2012-07-26 12:07:23

+0

不,如果從stackoverflow中選擇「st」,它不起作用。你試過了嗎? – Zsolt 2012-07-26 12:09:54