【问题标题】:WPF bug: TextRange.GetPropertyValue in RichTextBoxWPF 错误:RichTextBox 中的 TextRange.GetPropertyValue
【发布时间】:2012-07-26 19:57:45
【问题描述】:

我必须在 WPF RichtTextBox 中检索所选文本的 Foreground 属性,但我认为 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());
    }

当我选择 stackoverflow 超链接的前几个字符时,MessageBox 说 Foreground 属性是一个 DependencyProperty.UnsetValue,但是当我选择链接的其他部分时,它显示的是真正的前景色。

它看起来像一个错误。

这个问题有解决办法吗?

【问题讨论】:

    标签: c# .net wpf richtextbox flowdocument


    【解决方案1】:

    这是因为 TextElement.ForegroundProperty。 如果你把它改成Hyperlink.ForegroundProperty,它会显示真实的值。

    【讨论】:

    • 是的,我已经尝试过了,但它不适合我。一切都一样。
    • 只有从两者中选择一个部分才有效。 (例如“运行运行 stackoverfl”)。
    • 不,如果您从 stackoverflow 中选择“st”,它将无法正常工作。你试过了吗?
    • 这很奇怪,因为如果我选择“s”,它说它是黑色的(但它不是,只有在那之前的运行)如果我选择“t”(来自stackoverflow的第二个字母),它说真正的前景画笔颜色。看起来它以某种方式“先于”选择。例如当你有一个单字母的超链接时,像这样:stackoverflow.com">B</Hyperlink> ....那么就没有办法选择真正的前景色了!
    • :) 我每次只选择链接的结尾。你是对的。
    猜你喜欢
    • 2022-08-14
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多