MDI tabcontrol focus

Topics: Bugs, UI Architecture
Oct 15, 2011 at 5:49 PM

In CM using WPF and the tab control for an MDI style UI, I have an intermittent issue where a tab item does not receive the focus when I activate it.  Before I used CM I built my own framework for the MDI and tab control and to solve the problem before I used the following statement.  

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (ThreadStart)(() => tabItem.Focus()));

First, does anyone know why this happens?  Why the tab item activates without receiving focus? Also, where do I put the above code?


I tried to put it on the activate processed event and the content control on the tabitem does not get a reference to its parent so I never get a referrence to the tabitem.

 void ShellViewModel_ActivationProcessed(object sender, ActivationProcessedEventArgs e)
           var viewModel = e.Item as IViewAware;

           if (viewModel != null)
              var view = viewModel.GetView() as DependencyObject;

              if (view != null)
                 var tabItem = UIHelper.FindVisualParent(view);

                 if (tabItem != null)
                    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (ThreadStart) (() => tabItem.Focus()));

Any help would be appreciated.

I am using the latest version of CM.

Oct 21, 2011 at 10:38 PM

Does anyone have any idea how to fix this?  

Oct 22, 2011 at 12:54 AM

I haven't ever encountered this I havent' a clue what is going on or how to fix it.

Oct 22, 2011 at 1:23 AM

No worries.  Not quite sure why it happens.   I can show you what I was doing before which worked.  

I would add the tab then set it as active.   Then I would need to set the focus as below.  It was intermittent before I added the set focus statement.  That solved it.

Now I am us CM and am not adding tab items in code I do not have control over the process. 

I have tried using the selection changed event on the tab control I am using as an item container, that did not work.

 public void AddTab(WorkspaceItem workspaceItem)

         workspaceItem.TabItem = new CloseableTabItem();
         workspaceItem.TabItem.Content = workspaceItem.Control;
         workspaceItem.TabItem.Header = workspaceItem.ViewModel.DisplayName;
         workspaceItem.TabItem.DataContext = workspaceItem.ViewModel;

         var binding = new Binding("DisplayName") { Source = workspaceItem.ViewModel, Mode = BindingMode.TwoWay };
         workspaceItem.TabItem.SetBinding(HeaderedContentControl.HeaderProperty, binding);


      public void SetActiveWorkspace(WorkspaceItem workspaceItem)
         workspaceItem.TabItem.IsSelected = true;

         var element = workspaceItem.TabItem as UIElement;
         WorkspaceTabControl.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (ThreadStart)(() => element.Focus()));