Why does the ItemsControl show the focus rectangle when its parent is focused?

The WPF ItemsControl will display the focus rectangle if it thinks it has focus and the user presses Tab or Alt.

But I recently had an ItemsControl displaying a focus rectangle even though it had no focus - one of its parents. The ItemsControl was inside a UserControl, which was inside another UserControl that had focus. Something like that:

<!-- UserControl1.xaml; this is the control that has focus -->
<UserControl x:Class="UserControl1" Focusable="True" ...>
    <UserControl2/>
</UserControl>

<!-- UserControl2.xaml -->
<UserControl x:Class="UserControl2">
    <ItemsControl .../>
</UserControl>

      

Or, to visually display nesting:

+---------------------------------------------------+
| UserControl1 (has focus)                          |
|                                                   |
| +-----------------------------------------------+ |
| | UserControl2                                  | |
| |                                               | |
| | +-------------------------------------------+ | |
| | | ItemsControl (shows focus rectangle)      | | |

      

It took me a while (and this StackOverflow question ) to figure out where the focus rectangle came from, because I never expected a control that didn't have focus to show the focus rectangle.

I'm still researching my way around WPF and obviously I don't know enough yet, or it wouldn't confuse me. Two questions to help me understand:

  • Why does the ItemsControl display the focus rectangle when it doesn't actually have focus, but one of its parent controls? What is the reason for this feature? (I'm sure there is one - maybe something to do with templates or visual trees? - I don't have a deep understanding of the mechanics and philosophy of WPF yet.)
  • How it works? What mechanism does the ItemsControl use to decide that it should display the focus rectangle?
+1


source to share


2 answers


Are you sure the ItemsControl has no focus? If it draws focus straight, it should. Just because the controls above in the visual tree are focused does not mean that one of its children is not focused either. To understand why, make sure you understand the difference between "boolean focus" and "keyboard focus" in WPF. MSDN has an exaustive explain .



+1


source


I think this is by design. I understand that focus is inherited from the parent control and works with it in the visual tree. Try examining the visual style of the itemscontrol parent and then the control itself to see what's going on with the visual xaml tree. This should show some clues as to what's going on. You can also try searching Google and some other sites for information on focus and element management, etc. There may be some other information that gives more details on how this all works than I can explain it. What happens if you click on the control and then click elsewhere and then select the parent? Any focus change? You can also try to create your own class that overrides focus and makes it like this,so that the control doesn't show focus in the same way. It should be (fairly) easy to do. There may be information about this on certain sites / blogs.



0


source







All Articles