[Version 2.0] Programatic LayoutAnchorable Causes Null Exception When Docking

Feb 15, 2015 at 7:47 AM
Hi:

I'm trying to programmatically add layout anchorables to my bare dockingmanager. When I do, things look fine, until I undock a pane and try to redock it. I get a null exception that looks like this [1].

The code I'm using to create this is as such - am I missing something? Thanks.

Xaml:
<DockPanel>
    <avalonDock:DockingManager x:Name="dockingManager">
        <avalonDock:LayoutRoot>
            <avalonDock:LayoutRoot.RootPanel>
                <avalonDock:LayoutPanel Orientation="Horizontal">
                </avalonDock:LayoutPanel>
            </avalonDock:LayoutRoot.RootPanel>
        </avalonDock:LayoutRoot>
    </avalonDock:DockingManager>
</DockPanel>


Code for adding panel:
    private void AddNewPane(string friendlyName, object paneContent)
    {
        var newPane = new LayoutAnchorable();
        newPane.Title = friendlyName;
        newPane.IsActive = true;
        newPane.IsSelected = true;
        newPane.CanFloat = true;
        newPane.CanClose = true;
        newPane.CanHide = true;
        newPane.Content = paneContent;
        newPane.ContentId = friendlyName + DateTime.Now.ToString();
        newPane.AddToLayout(m_DockingManager, AnchorableShowStrategy.Top | AnchorableShowStrategy.Most | AnchorableShowStrategy.Right);
    }


[1] Null exception after docking:

System.NullReferenceException was unhandled
_HResult=-2147467261
_message=Object reference not set to an instance of an object.
HResult=-2147467261
IsTransient=false
Message=Object reference not set to an instance of an object.
Source=Xceed.Wpf.AvalonDock
StackTrace:
   at Xceed.Wpf.AvalonDock.Controls.DropTarget`1.<>c__DisplayClass1.<Drop>b__0()
   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)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(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)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at Editor.App.Main() in blahblah
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Jan 29, 2016 at 7:53 PM
I have found the issue with this.
On an add the current property settings are probably the cause.
My issue occurred due to the IsActive Property being set to true before adding it to the screen.
AKA My fix:
    newPane.IsActive = true;  =>         newPane.IsActive = false;
I had almost the exact same stack trace. Took some experimentation to figure out the cause.
You maybe able to toggle the IsActive to true after the addition of the newPane to the layout.