WPF Focus In Tab Control Content On New Tab Creation

I've searched SO and Google a lot around this issue but can't find anything else to try.

I have a MainView (window) containing a tab control. The tab control is bound to an ObservableCollection of child items (custom controls). The MainView ViewModel has a method that allows you to add ChildViews to the collection, which then creates a new tab. When a new tab is created it becomes the active tab and that works great. This method in MainView is called from another ViewModel (OtherViewModel).

What I'm trying to do is set the keyboard focus to the first control on the tab (AutoCompleteBox from WPFToolkit *) when creating a new tab. I also need to set focus in the same way, but WITHOUT creating a new tab (so set focus to the currently active tab).

(* Note that there seems to be some focus issues with the AutoCompleteBox - even if it does have focus, you need to send MoveNext () to get the cursor in its window. I've worked on this already).

So here's the problem. Focusing works when I don't create a new tab, but it doesn't work when I create a new tab. Both functions use the same method to set focus, but the creation logic first calls the method that creates a new tab and sets it active. Code that sets focus (in ChildView Codebehind):

        IInputElement element1 = Keyboard.Focus(autoCompleteBox);
        //plus code to deal with AutoCompleteBox as noted.

      

In any case, Keyboard.FocusedElement starts as MainView. After making the call, the call to Keyboard.Focus doesn't seem to do anything (the focused item is still the MainView). Calling this without creating the tab correctly sets the keyboard focus to autoCompleteBox.

Any ideas?

Update:

Bender's proposal half worked.

So now in both cases the focused element is the correct AutoCompleteBox. What I then do is MoveNext (), which sets focus to the TextBox. I am assuming that this textbox is internal to the AutoCompleteBox as the focus was correctly set on the screen when this happened. Now I'm not sure. This is still the behavior I see when this code hits when it does NOT do the creation. Once created, MoveNext () sets focus to the element back to my MainView.

The problem should still be in line with Bender's answer, where the state of the controls is independent of whether a new tab was created or not. Any other thoughts?

Final update

As noted, mayocha's suggestion worked.

I wanted to update this in case someone else happened to have the same issue with AutoCompleteBox. It looks like setting focus doesn't activate it in the UI - you need to do MoveNext to move focus forward once to the control's inner textarea. This is based on my debugging experience which cannot be 100% scientific. If I have time, I will try to create a small project to reproduce and send it to the WPFToolkit team.

+1


a source to share


2 answers


You can try deferring the focus change with

Dispatcher.BeginInvoke(MyChangeFocusAction, DispatcherPriority.ContextIdle);

      



It will be queued after layout completes and property updates. I don't think this is the best practice, but it works for me.

+3


a source


The control must be visible for focus, you can try to defer focus by subscribing to the IsVisibleChanged event, something similar to the following should work:



public static void setFocusLate(this Control control)
        {
            DependencyPropertyChangedEventHandler handler = null;

            handler = delegate
                {
                    control.Focus();
                    control.IsVisibleChanged -= handler;
                };

            control.IsVisibleChanged += handler;
        }

      

+1


a source







All Articles