Esta documentación está archivada y no tiene mantenimiento.

ContentElement.Focus (Método)

Attempts to focus the keyboard on this element.

Espacio de nombres: System.Windows
Ensamblado: PresentationCore (en presentationcore.dll)

public bool Focus ()
public final boolean Focus ()
public final function Focus () : boolean
No se pueden utilizar métodos en XAML.

Valor devuelto

true if keyboard focus could be set to this element; false if this method call did not force 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.

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

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, this happens due to user input. For more information, see Input Overview.

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.

void FocusOnParagraph(object sender, RoutedEventArgs e)
  ContentElement ce = this.FindName("focusableP") as ContentElement;

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0