Wpf which control has focus




















Active 1 year, 1 month ago. Viewed 57k times. Improve this question. Dave Clemmer 3, 12 12 gold badges 47 47 silver badges 72 72 bronze badges.

Honzajscz Honzajscz 2, 1 1 gold badge 24 24 silver badges 23 23 bronze badges. Add a comment. Active Oldest Votes. It depends on the type of focus you are after, Logical or Keyboard. Keyboard focus refers to the element that currently receives keyboard input. Only one element in the entire desktop can have keyboard focus. Logical focus refers to the element in a focus scope that would receive the keyboard input, if the focus scope was active.

Improve this answer. Ben Thank you for the advise. I end up using the first option with combination of Application. Windows[0] instead of 'this'. One thing I noticed a while back was that the commits to my work Git repos were using Daniel Schroeder's Programming Blog Sharing my tips, tricks, and code to help other developers be more productive.

Daniel Schroeder aka deadlydog. Here is a quick checklist to go through: is the control you are trying to set focus to Enabled , Visible , Loaded , and Focusable. If any of these properties are false, you cannot set focus to the element.

If the control that currently has focus overrides the PreviewLostFocus event, it can set e. Handled to true to prevent other controls from stealing focus from it. Leave a Comment Your email address will not be published. Website optional. Not used. Leave blank if you are a human. There can be only one element on the whole desktop that has keyboard focus.

The static property FocusedElement on the Keyboard class gets the element that currently has keyboard focus. In order for an element to obtain keyboard focus, the Focusable and the IsVisible properties on the base elements must be set to true. Some classes, such as the Panel base class, have Focusable set to false by default; therefore, you must set Focusable to true if you want such an element to be able to obtain keyboard focus.

Keyboard focus can be obtained through user interaction with the UI, such as tabbing to an element or clicking the mouse on certain elements. Keyboard focus can also be obtained programmatically by using the Focus method on the Keyboard class.

The Focus method attempts to give the specified element keyboard focus. The returned element is the element that has keyboard focus, which might be a different element than requested if either the old or new focus object block the request. The following example uses the Focus method to set keyboard focus on a Button. The IsKeyboardFocused property on the base element classes gets a value indicating whether the element has keyboard focus.

The IsKeyboardFocusWithin property on the base element classes gets a value indicating whether the element or any one of its visual child elements has keyboard focus. When setting initial focus at application startup, the element to receive focus must be in the visual tree of the initial window loaded by the application, and the element must have Focusable and IsVisible set to true.

The recommended place to set initial focus is in the Loaded event handler. A Dispatcher callback can also be used by calling Invoke or BeginInvoke. Logical focus refers to the FocusManager. FocusedElement in a focus scope. A focus scope is an element that keeps track of the FocusedElement within its scope. When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus.

This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.

There can be multiple elements that have logical focus in an application, but there may only be one element that has logical focus in a particular focus scope.

In code, an element can be turned into a focus scope by calling SetIsFocusScope. The following example makes a StackPanel into a focus scope by setting the IsFocusScope attached property. GetFocusScope returns the focus scope for the specified element.

GetFocusedElement gets the focused element for the specified focus scope. SetFocusedElement sets the focused element in the specified focus scope. SetFocusedElement is typically used to set the initial focused element.

The following example sets the focused element on a focus scope and gets the focused element of a focus scope.



0コメント

  • 1000 / 1000