xceedsoftware/wpftoolkit

InvalidOperationException When Floating A Document Containing A CustomControl With Focus() In It's OnApplyTemplate()

mwagnerEE opened this issue · 0 comments

While looking to see if this issue was addressed, I feel like I've seen similar issues with focusing or using Keyboard.Focus(). However most of those caused a NullReferenceException.

I am getting an InvalidOperationException when I drag the tab of a document to cause it to float. It seems to occur when the document contains a CustomControl which calls Focus() within it's OnApplyTemplate() method.

It seems to happen because when focusing I think the Dispatcher suspends messages but LayoutFloatingWindowControl sends a message in its OnActivated method.

I was running a local copy of AvalonDock when I encountered it and I changed the code to:

Application.Current.Dispatcher.BeginInvoke(()=> Win32Helper.SendMessage( windowHandle, Win32Helper.WM_NCLBUTTONDOWN, new IntPtr( Win32Helper.HT_CAPTION ), lParam ));

and it works for my purposes so far, although I have no idea if there will be any side effects?

It is extremely simple to reproduce. Attached is a folder containing the solution, a README explaining basically what I said here, and a file called "InvalidOperationException.txt" containing the exception info and CallStack of the error.
AvalonDockError.zip

Also, I believe this is completely unrelated, but while looking into this issue, I found that Microsoft discourages the use of SendMessage without checking InSendMessage to avoid deadlocks. Messages are a little over my head though and again I believe this to be unrelated as I've tried this and InSendMessage returned false but the error still occurred.