元素已经是另一个元素的子元素
我的 Silverlight 应用程序中出现以下错误。但我无法弄清楚问题出在哪个控制上。如果我调试它不会破坏代码中的任何内容,它只会在仅包含框架代码的框架调用堆栈中失败。在这种情况下,有什么方法可以获取有关 Silverlight 应用程序的哪个部分出现问题的更多信息。
Message: Sys.InvalidOperationException: ManagedRuntimeError error #4004 in control 'Xaml1': System.InvalidOperationException: Element is already the child of another element. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value) at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value) at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value) at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value) at System.Windows.PresentationFrameworkCollection`1.Add(T value) at System.Windows.Controls.AutoCompleteBox.OnApplyTemplate() at System.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)
上下文中 AutoCompeletBox 的 XAML 为:
<tk:AutoCompleteBox
x:Name="acName"
Grid.Row="0"
Grid.Column="1"
LostFocus="acName_LostFocus"
Height="20"
Width="80"
HorizontalAlignment="Left"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该错误是一个通用的包罗万象的异常,有很多原因。我编写了一个调试器实用程序,可以帮助确定 XAML 的哪一部分实际上导致了错误。您可以从我的博客下载它: http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/
The error is a generic catch-all exception that has many many causes. I've written a debugger utility that can help to identify which part of the XAML is actually causing the error. You can download it from my blog: http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/
您的项目可能是视觉元素,而不是数据对象。
如果您提供 XAML,我可以帮助确保情况确实如此。
Your items may be visual elements, instead of data objects.
If you provide the XAML, I can help make sure that is the case.
通常,当所述元素已经附加到现有父级并且您在代码中的某个位置尝试重新设置它的父级时,就会发生此错误(即,当您必须首先从父级中删除子级时,只需直接“添加”,然后将其添加到孩子等)。
具体来说,如果控制失败,上述信息不足以消化。
Usually this error occurs when the said Element is already attached to an existing Parent and somewhere in your code you're attempting to re-parent it (ie via just straight "add" when you in turn must remove the child from the parent first, then Add it to the children etc).
Where specifically the control is failing the above isn't enough info to digest.
简单而愚蠢的解决方案:
Simple and stupid solution: