开发者

Element is already the child of another element

开发者 https://www.devze.com 2022-12-25 07:09 出处:网络
I get the folowing error in my Silverlight application. But i cant figure out what control it is that is the problem. If i debug it don\'t break on anything in the code, it just fails in this framewor

I get the folowing error in my Silverlight application. But i cant figure out what control it is that is the problem. If i debug it don't break on anything in the code, it just fails in this framework callstack with only framework code. Is there any way to get more information on what part of a Silverlight app that is the problem in this case.

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 Syst开发者_JAVA技巧em.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)

The XAML for the AutoCompeletBox that is in the context is:

<tk:AutoCompleteBox 
    x:Name="acName" 
    Grid.Row="0" 
    Grid.Column="1" 
    LostFocus="acName_LostFocus"
    Height="20"
    Width="80" 
    HorizontalAlignment="Left"/>


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/


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:

public class AutoCompleteTextBox : AutoCompleteBox
    {
        public override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }
            catch { }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消