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

Open
#1,745 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
desktop

Research direction

Start with LayoutFloatingWindowControl.OnActivated and the attached README and InvalidOperationException.txt. Reproduce the failure by floating a document containing a CustomControl whose OnApplyTemplate calls Focus(), then use the provided call stack to trace the message handling. Done means the document floats without the InvalidOperationException and existing floating behavior remains intact.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C#
Stars
4.2k
Forks
912
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xceedsoftware/wpftoolkit

All issues in xceedsoftware/wpftoolkit

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.