【问题标题】:InvalidOperationException when DataGrid loses focus to a HyperLink当 DataGrid 失去对超链接的焦点时出现 InvalidOperationException
【发布时间】:2011-08-19 23:24:18
【问题描述】:

我的 Siverlight4 应用程序中恰好遇到了这个问题:

说明 当数据网格失去对超链接(RichTextBox 的一部分)的焦点时会出现错误。

异常详情如下:

{
System.InvalidOperationException: Reference is not a valid visual DependencyObject.
at System.Windows.Media.VisualTreeHelper.GetRelative(DependencyObject reference, RelativeKind relativeKind)
at System.Windows.Media.VisualTreeHelper.GetParent(DependencyObject reference)
at System.Windows.Controls.DataGrid.DataGrid_LostFocus(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
}

要重现此错误,请放置一个包含一些数据的 DataGrid,以及一个包含至少一个超链接的 RichTextBox,RichTextBox 必须具有 IsReadOnly="True" ,并在运行时将焦点放在数据网格上,单击超链接。

该错误在文件 DataGrid.xaml.cs 中,第 4782 行:DependencyObject parent = VisualTreeHelper.GetParent(focusedDependencyObject);

发件人:http://silverlight.codeplex.com/workitem/7696

解决方法似乎是编辑 Silverlight DLL,我不想这样做。

编辑:看起来单击任何地方都会引发此异常。

还有什么其他想法可以解决这个问题吗?

【问题讨论】:

    标签: c# silverlight-4.0 datagrid hyperlink richtext


    【解决方案1】:

    为什么 RichTextBox 必须是只读的?如果它不是只读的会怎样?

    如果这是 datdgrid 中的一个已知错误,那么我会考虑破解一个 if-it-looks-right-it-is-right 解决方案(我认为这胜过修改/修复 dll)。

    例如,它必须是超链接吗?是否可以改为文本块(您将在代码中处理超链接部分)?

    【讨论】:

    • 它必须是只读的,因为用户不应该编辑它。 RichTextBox 必须显示具有所有漂亮格式的 FlowDocument。我不能将其限制为 TextBox。
    【解决方案2】:

    我们现在想出了一个解决方法。

    当我们绑定到 FlowDocument 的 XALM 字符串时,我们向模型添加了一个新属性,以便 RichtTextBox 绑定到。在该属性中,我们手动删除超链接信息并将其替换为 BOLD 标记。

    public string BodyXamlWithOutHyperLink
            {
                get
                {
                    const string RegExPattern1 = @"<Hyperlink \S*"">";
                    const string RegExPattern2 = @"</Hyperlink>";
    
                    string body = this.BodyXaml;
    
                    if (string.IsNullOrEmpty(body))
                    {
                        return string.Empty;
                    }
    
                    Regex bodyRegEx = new Regex(RegExPattern1);
                    body = bodyRegEx.Replace(body, "<Bold>");
                    bodylRegEx = new Regex(RegExPattern2);
                    body= bodyRegEx.Replace(mail, "</Bold>");
    
                    return body;
                }
    
                set
                {
                   // can be ignored, we are read-only anyway
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-05
      • 2013-04-03
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2013-06-12
      相关资源
      最近更新 更多