简体   繁体   English

WPF错误:RichTextBox中的TextRange.GetPropertyValue

[英]WPF bug: TextRange.GetPropertyValue in RichTextBox

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. 我必须在WPF RichtTextBox中检索所选文本的Foreground属性,但我认为TextRange.GetPropertyValue函数中存在错误。 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" />

And in the code behind: 并在后面的代码中:

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

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. 当我选择stackoverflow超链接的前几个字符时,MessageBox会说Foreground属性是DependencyProperty.UnsetValue,但是当我选择链接的其他部分时,它将显示真正的前景色。

It looks like a bug. 看起来像个错误。

Are there any workaround for this problem? 有没有解决此问题的方法?

It's because of the TextElement.ForegroundProperty. 这是因为TextElement.ForegroundProperty。 If you change it to Hyperlink.ForegroundProperty , it will show the true value. 如果将其更改为Hyperlink.ForegroundProperty ,它将显示真实值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM