Default Focused Element in Documents

Sep 24, 2008 at 6:40 AM
Edited Sep 24, 2008 at 6:42 AM

By default when you Ctrl+Tab to another document or simply click on a tab,  activated document content receives focus. This doesn’t work for me, because document content may be a Grid with different elements within. I wanted some certain element to receive focus when a document becomes active. To solve this I did the following modifications to AvalonDock components:

In ManagedContent class:

I’ve added a new DependencyProperty:

public static readonly DependencyProperty DefaultFocusedElementProperty = DependencyProperty.Register("DefaultFocusedElement", typeof(IInputElement), typeof(ManagedContent));


public Control DefaultElement


  get { return (Control)GetValue(DefaultFocusedElementProperty);}

  set { SetValue(DefaultFocusedElementProperty, value);}



Modified IsActiveContent property setter. Added another else if:


else if(DefaultElement != null)


  Debug.WriteLine("Try to set kb focus to " + DefaultElement);

  IInputElement kbFocused = Keyboard.Focus(DefaultElement);

  if (kbFocused != null)

    Debug.WriteLine("Focused element " + kbFocused);


    Debug.WriteLine("No focused element");



In DockingManager:


Modified ActiveDocumentProperty setter:


if (value.ContainerPane is DocumentPane && _activeDocument != value)


  List<ManagedContent> listOfAllDocuments = FindContents<ManagedContent>();

  listOfAllDocuments.ForEach((ManagedContent cnt) =>


    cnt.IsActiveDocument = cnt == value;

    cnt.IsActiveContent = cnt == value; // set focus to document content



  _activeDocument = value;




After that when a document becomes active a specified control recieves focus. I haven’t tested this cahnges a lot, so it might not work in some situations. Also, this approach doesn’t work well with List Views, because it would be better to set focus on a List View Item instead of a List View itself. Thoughts?

Sep 25, 2008 at 12:32 PM
Edited Sep 25, 2008 at 12:36 PM
Hi, thanks for your code. I had no time at moment to test but should work for me. Why do you think it's not working with ListViews? I mean one can decide to set focus on the listview as well on its selecteditem.

Sep 25, 2008 at 12:59 PM
Hi, yes, i think it is possible to bind DefaultFocusedElement property to some ListView property, that would return IInputElement of a selected item. I'll try it later and post here my results...