This project has moved and is read-only. For the latest updates, please go here.

How to set focus to an AvalonDock LayoutContent item programmatically

Mar 16, 2017 at 11:44 PM
Edited Mar 16, 2017 at 11:45 PM
I'm trying to implement something similar to Visual Studio's "View" menu where when you select a hidden item, it shows it and if you select an item already visible it gives it focus.

The hidden part was simple -- I just find the LayoutAnchorable with a matching ContentId from DockingManager.Layout.Hidden and call .Show() on that object.

The problem I'm running into is how to give focus to already visible items. I've written code to grovel through the tree to find the matching LayoutContent which has a matching ContentId, but how do I give it focus? There's no .Show() like there is for LayoutAnchorable, and I've tried setting IsActive = true and IsSelected = true and these aren't giving the content focus.

What am I missing here?
Mar 17, 2017 at 12:59 AM
Edited Mar 17, 2017 at 1:04 AM
I got the answer by looking at NavigatorWindow. Posting here in case anybody needs to do this in the future.
  1. Call DockingManager.Layout.Descendents().OfType<LayoutAnchorable>() to find the LayoutAnchorable that has a ContentId matching the expected one
  2. Get the LayoutItem by calling DockingManager.GetLayoutItemFromModel()
  3. Call LayoutItem.ActivateCommand.Execute(null) to select the item and give it focus.
Rinse and repeat for LayoutDocument.