views:

450

answers:

2

WPF's ItemsControl will display a focus rectangle when it thinks it has focus and the user presses Tab or Alt.

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

<!-- 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 show the nesting visually:

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

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

I'm still learning my way around WPF, and obviously I don't know enough yet, or this wouldn't have confused me. Two questions to help me understand:

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

I think this is by design. My understanding is that the focus is inherited from the parent control and works its way down the visual tree. Try examining the visual style of the itemscontrol parent and then the itemscontrol itself to see what is going on with the visual tree of xaml. That should reveal some clues as to what is happening. Also you can try searching google and some other sites for info on focus and itemscontrol, etc. There may be some other info out there that gives more details on how that all works that is clearer than how I can explain it. What happens if you click the itemscontrol and then click somewhere else and then click the parent? Any change in focus? You can also try making a custom class that overrides focus and make it so the control doesnt show focus the same way. Should be (fairly) easy to do. There may be info on that on certain sites/blogs.

+1  A: 

Are you sure that the ItemsControl doesn't have focus? If it's drawing a focus rect, it should. Just because a control higher in the visual tree is focused, doesn't mean that one of its children is not also focused. To understand why, make sure you have learned the difference between "logical focus" and "keyboard focus" in WPF. There is an exaustive explanation on MSDN.

PeterAllenWebb