WPF 错误:RichTextBox 中的 TextRange.GetPropertyValue [英] WPF bug: TextRange.GetPropertyValue in RichTextBox
问题描述
我必须在 WPF RichtTextBox 中检索所选文本的 Foreground 属性,但我认为 TextRange.GetPropertyValue 函数中存在错误.我编写了一个简单的应用程序来测试错误:
I have to retrieve the Foreground property of the selected text in a WPF RichtTextBox, but I think, there is a bug in the TextRange.GetPropertyValue function. I've written a simple application to test the error:
<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());
}
当我选择 stackoverflow 超链接的前几个字符时,MessageBox 说 Foreground 属性是一个 DependencyProperty.UnsetValue,但是当我选择链接的其他部分时,它显示了真正的前景色.
When I select the first few characters of the stackoverflow hyperlink, the MessageBox says that the Foreground property is a DependencyProperty.UnsetValue, but when I select other parts of the link, it shows the true foreground color.
它看起来像一个错误.
是否有解决此问题的方法?
Are there any workaround for this problem?
推荐答案
这是因为 TextElement.ForegroundProperty.如果将其更改为Hyperlink.ForegroundProperty
,它将显示真实值.
It's because of the TextElement.ForegroundProperty.
If you change it to Hyperlink.ForegroundProperty
, it will show the true value.
这篇关于WPF 错误:RichTextBox 中的 TextRange.GetPropertyValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!