【问题标题】:WPF bug? Applying UnsetValue to the Foreground property in HyperlinksWPF错误?将 UnsetValue 应用于超链接中的 Foreground 属性
【发布时间】:2012-07-24 22:46:29
【问题描述】:

我目前正在 WPF 中开发一个可突出显示的富文本框控件。用户可以在搜索框中键入一些文本,富文本框突出显示搜索词的出现。 一切正常,直到我在richtextbox中有超链接,例如:

blablabla stackoverflow.com

然后我搜索这个词:

st

它以红色前景正确突出显示文本(stackoverflow 中的“st”)。 但是当我按下退格键时——搜索词变成了单个“s”字母——我得到了一个例外:

System.ArgumentException: 'NamedObject' parameter type is not valid for formatting property 'Foreground'.
Parameter name: value

这是因为我的算法是这样的:

  1. 突出显示richtextbox 中的文本:我将每个突出显示的TextRange 添加到字典中,其中键是突出显示的TextRange,值是我使用以下代码检索的那个TextRange 的旧前景属性:

    textrange.GetPropertyValue(TextElement.ForegroundProperty)
    
  2. 每次用户更改过滤器字符串时,我都会删除旧的突出显示,并使用新的过滤器字符串“重新突出显示”richtextbox,就像在第 1 步中一样。

    • 异常来自过程的“删除突出显示”部分。为了删除突出显示,我枚举了字典,并且对于每个 TextRange,我将 ForegroundProperty 重置为保存的值:

      foreach (var textRangeEntry in highlightedTexts)
      {                
          textRangeEntry.Key.ApplyPropertyValue(TextElement.ForegroundProperty, textRangeEntry.Value);
      }
      

问题是有时保存的 ForegroundProperty 会变成 DependencyProperty.UnsetValue - 我得到一个异常。

这很奇怪。它看起来像 ForegroundProperty “不喜欢” UnsetValue,但例如,当我过滤到

ov

在正文中

blablabla stackoverflow.com

然后当我擦除“v”时,一切正常。

只有在我突出显示超链接的开头时才会出现异常。

这是 WPF 中的一个奇怪错误还是我做错了什么?

【问题讨论】:

    标签: wpf richtextbox flowdocument


    【解决方案1】:

    我已经意识到这个问题(错误?)在 textrange.GetPropertyValue 调用中。我会以更简短、更具体的形式重新发布这个问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-04
    • 2011-01-09
    • 2012-02-03
    • 1970-01-01
    • 2014-10-17
    • 2012-07-09
    • 2011-11-06
    • 1970-01-01
    相关资源
    最近更新 更多