开发者

Silverlight: How do I determine how my control got the focus?

Specifically, I want to know if the co开发者_如何学Pythonntrol got the focus because the user tabbed to it or because the user clicked on it.


I was thinking about this. While I don't have a bold answer for you, I was thinking of a few ways to do it. The problem, as you know, is that the KeyDown and MouseLeftButton down events happen after GotFocus. I also couldn't find a way to check the state of the mouse and keyboard during the GotFocus event.

You could add a new TabFocus and ClickFocus event and fire them off on the click or tab event.

On your gotFocus event, set a flag on the userControl.

justGotFocus = true;

add events public event TabFocus, ClickFocus (metacode)

And then in the Mouse and Key events, you

TabFocus(this, new EventArgs())

ClickFocus(this, new EventArgs())

or just call a central function with a FocusType parameter

public event SpecificFocusEvent SpecificFocus(this, FocusType.TabKey)

Sorry... tired... horrible code =P

You could put the above into an attached property to reuse.

OR there's a flag somewhere I haven't found yet =P

pj

EDIT

You are on the right track, just a little out of order. Set the flag on the MouseDown events, read it with GotFocus, and clear it in MouseUp/GotFocus/LostFocus. It is ugly as hell, but but it usually works.


By Control, do you mean a UserControl?

If though, the event GotFocus of the UserControl is raised when a (input)control, like TextBox on the UserControl gets focus.

The UserControl by itself cannot have the focus (by clicking on the background of the UserControl).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜