locked
WPF TabItem doesn't get selected RRS feed

  • Question

  • I got a strange issue in WPF MDI(Multiple-document interface). I am using TabControl, TabItem. My requirement is whenever TabItem is opened it should get selected , I mean TabItem header should get selected. In fact I have written 
    my logic just to achieve this. TabItem selection is crucial for me as that selection prepares a dynamic menu and updates the menu bar.

    TabItem has scroll viewer which hosts controls like edit box , grid etc. For normal case everything is happening fine.
    But for some cases , tabitem is not getting selected. It renders scrollviewer content properly only problem is 
    somehow , tab header doesn't get selected. As per my observation , if scrollviewer doesn't contain any control or contains non editable controls I see this issue. But still for these usecases , I can say this is intermittent.

    To know the root cases ,I have added focus events for both tabitem and scrollviewer. To my surprise , while 
    opening the tabitem at the end focus is lost for scrollviewer and then for tabitem too. I don’t have any idea of 
    who is causing the focus lost events. Also I don’t see any logic which may kick these focus lost events. 

    As per the suggestion mentioned in below link , I added code changes but there is no improvement.

     https://social.msdn.microsoft.com/Forums/vstudio/en-US/30ed27ce-f7b7-48ae-8adc-0400b9b9ec78/tabcontrol-focus-problems

    Could anyone add hints on this ?

    Thanks,
    Brahmaji.  
    Thursday, December 10, 2015 6:50 AM

Answers

  • Douglas' deferred approach doesn't defer as much as I would.

    And all that updatelayout stuff.. I don't really like the look of it.

    I would just try:

    Application.Current.Dispatcher.InvokeAsync(new Action(() => 
    {  
       // Do stuff
    
    }), DispatcherPriority.ContextIdle);

    That waits until anything else the ui is doing has finished.

    I guess it's rendering this, focussing that and then decides to go lose focus doing something else ( I don't follow what ).

    Whilst it's doing stuff it will not be idle.

    That snippet defers the "Do stuff" until it's doing nothing.

    .

    Try that.

    If it doesn't resolve the issue then maybe we need to delve more into what you're doing. 


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles



    • Edited by Andy ONeill Thursday, December 10, 2015 7:55 AM
    • Proposed as answer by Xavier Xie-MSFT Wednesday, December 16, 2015 5:46 AM
    • Marked as answer by Xavier Xie-MSFT Wednesday, December 23, 2015 11:20 AM
    Thursday, December 10, 2015 7:54 AM

All replies

  • Douglas' deferred approach doesn't defer as much as I would.

    And all that updatelayout stuff.. I don't really like the look of it.

    I would just try:

    Application.Current.Dispatcher.InvokeAsync(new Action(() => 
    {  
       // Do stuff
    
    }), DispatcherPriority.ContextIdle);

    That waits until anything else the ui is doing has finished.

    I guess it's rendering this, focussing that and then decides to go lose focus doing something else ( I don't follow what ).

    Whilst it's doing stuff it will not be idle.

    That snippet defers the "Do stuff" until it's doing nothing.

    .

    Try that.

    If it doesn't resolve the issue then maybe we need to delve more into what you're doing. 


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles



    • Edited by Andy ONeill Thursday, December 10, 2015 7:55 AM
    • Proposed as answer by Xavier Xie-MSFT Wednesday, December 16, 2015 5:46 AM
    • Marked as answer by Xavier Xie-MSFT Wednesday, December 23, 2015 11:20 AM
    Thursday, December 10, 2015 7:54 AM
  • Perhaps creating a property in the DataContext bound to the IsSelected property of the TabItem and creating breakpoints in the property get and set accessors could give you a path to see in the CallStack window what is going on in the background.

    Codelines

    Friday, December 11, 2015 10:58 AM
  • That's an idea.

    Focus issues are very ephemeral though.

    And a break point will take focus.

    debug,writeline might be more useful.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Friday, December 11, 2015 12:33 PM
  • Thanks for the hints. Main challenge here is , I could not replicate the problem here. I just saw this in one of my customers environment and he is worrying about this weird behavior. In fact I have added few trace messages and got them tested by customer. But I don't see any unexpected thing with the logs.

    One  idea I have in mind is  , after the tab item which displays  data gird is loaded  I am trying to programmatically select the tab. Even this also doesn't help , I think simply calling " tab.isSelected = true " is not enough. 

    Any other idea you guys have? At least if I am able to select the tab through code as a last activity then it may select the tab. Well , this  makes sense from theory point of view but practically not sure whether this is a good approach.

    Thanks,

    Brahmaji.

    Thursday, December 17, 2015 7:07 AM