.Focus Method ()
.NET Framework (current version)
Attempts to set focus to this element.
PresentationCore (in PresentationCore.dll)
System.Boolean true if keyboard focus and logical focus were set to this element; false if only logical focus was set to this element, or if the call to this method did not force the focus to change.
To be focusable,
and Focusable must both be IsEnabled true.
Even if an element is focusable and enabled, event handling within a specific tree, (such as for a composite control) might respond to the preview focus events by not allowing focus there, thus this method would return
Focus in general is governed by two separate concepts: keyboard focus and logical focus, which are not always identical. For more information, see
Focus Overview or Input Overview.
Focus returns true, and IsKeyboardFocused are also IsKeyboardFocusWithin true.
If the related properties are not already
true, when you call Focus, one or more of the following events are raised in the following order: PreviewLostKeyboardFocus, PreviewGotKeyboardFocus (source is the new focus target), IsKeyboardFocusedChanged, IsKeyboardFocusWithinChanged, LostKeyboardFocus, GotKeyboardFocus (source is the new focus target).
In order for this call to be successful, some other element in the application needed to have focus previously.
The following example sets focus to a
TextBox referenced by , and then adjusts the position of the cursor within the Name TextBox.
void OnClickMoveToStart( object sender, RoutedEventArgs e)
.NET Framework Available since 3.0 Return to top