Expand Minimize

ContentElement.Focus Method

Attempts to set focus to this element.

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)

'Declaration
Public Function Focus As Boolean

Return Value

Type: System.Boolean
true if keyboard focus could be set to this element; false if this method call did not force focus.

Implements

IInputElement.Focus

To be focusable, Focusable and IsEnabled must both be true. Note that nearly all ContentElement derived classes are not Focusable by default.

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 false.

Focus in general is governed by two separate concepts: keyboard focus and logical focus, which are not always identical. This method sets the logical focus. There is no programmatic means to set keyboard focus specifically; keyboard focus is determined by user input. For more information, see Focus Overview and Input Overview.

If calling Focus returns true, IsKeyboardFocused and IsKeyboardFocusWithin are also 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).

The following example is a page-loaded event handler that finds a specified named paragraph in the document and sets focus to it. Paragraphs are not focusable by default; this particular paragraph had a style applied (not shown) that used a style Setter to make it focusable.

	Private Sub FocusOnParagraph(ByVal sender As Object, ByVal e As RoutedEventArgs)
	  Dim ce As ContentElement = TryCast(Me.FindName("focusableP"), ContentElement)
	  ce.Focus()
	End Sub

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft