[Version 1.3] "Element already has a logical parent"

Dec 6, 2011 at 7:11 PM

We've begun running into the following exception in our WPF application.  It happens when we right-click a UserControl that has no event handler for a right-click.  The UserControl is hosted inside an "AvalonDock:DocumentContent".


{"Element already has a logical parent. It must be detached from the old parent before it is attached to a new one."}
    [System.InvalidOperationException]: {"Element already has a logical parent. It must be detached from the old parent before it is attached to a new one."}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    InnerException: null
    Message: "Element already has a logical parent. It must be detached from the old parent before it is attached to a new one."
    Source: "PresentationFramework"
    StackTrace: "   at MS.Internal.Controls.InnerItemCollectionView.AssertPristineModelChild(Object item)
   at MS.Internal.Controls.InnerItemCollectionView.Insert(Int32 index, Object item)
   at System.Windows.Controls.ItemCollection.Insert(Int32 insertIndex, Object insertItem)
   at AvalonDock.DocumentContent.Activate()
   at AvalonDock.ManagedContent.OnMouseDown(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)"
    TargetSite: {System.Windows.DependencyObject AssertPristineModelChild(System.Object)}


Has anyone else run into this?  Is it a known issue or problem?

Thanks!

Brad.

Dec 7, 2011 at 9:15 AM
Edited Dec 7, 2011 at 9:17 AM

Not an issue, may be a problem: It seems your UserControl wants to be attached to a new branch of the logical tree.

We have the case in Sofa and we make a check before, and if needed detach the UserControl before reattaching it:

sofaComponent.UserControl can be any UserControl. It can already be the Content (WPF Content object of a ContentControl) of a DockableContent:

DependencyObject parent = LogicalTreeHelper.GetParent(sofaComponent.UserControl);

if (parent != null)
{
    ((DockableContent)parent).Content = null;
}

Setting parent.Content to null does not set sofaComponent.UserControl to null but allows it to be Content of another ContentControl.

Hope it helps.

The Sofa team, SofaWPF library and AvalonDock Commercial support

May 27, 2013 at 9:39 AM
Hi plettb,

I know, I'm late with this, but did you ever figure out a solution for this problem? We have precisely the same issue.
In our case it might relate to list-virtualization within the user-control triggering a premature initialization of the component when the parent is not ready yet.

BR,
tom