Keyboard.GotKeyboardFocus Keyboard.GotKeyboardFocus Keyboard.GotKeyboardFocus Attached Event

定义

当元素接收键盘焦点时发生。Occurs when an element receives keyboard focus.

see AddGotKeyboardFocusHandler, and RemoveGotKeyboardFocusHandler
see AddGotKeyboardFocusHandler, and RemoveGotKeyboardFocusHandler
see AddGotKeyboardFocusHandler, and RemoveGotKeyboardFocusHandler

示例

下面的示例创建一个TextBox , 并附加事件和LostKeyboardFocus事件GotKeyboardFocus的事件处理程序。The following example creates a TextBox and attaches event handlers for the GotKeyboardFocus event and the LostKeyboardFocus event. 当获取键盘焦点时, 将更改背景色并清除的TextBox文本。 TextBoxWhen the TextBox obtains keyboard focus, the background color is changed and the text of the TextBox is cleared. TextBlock当失去键盘焦点时, 背景色会更改, 并调用方法来重置在示例中使用的变量。When the TextBlock loses keyboard focus, the background color is changed and a method is called which resets variables used in the sample.

<Border BorderBrush="Black" BorderThickness="1"
        Width="200" Height="100" Margin="5">
  <StackPanel>
    <Label HorizontalAlignment="Center" Content="Type Text In This TextBox" />
    <TextBox Width="175"
             Height="50" 
             Margin="5"
             TextWrapping="Wrap"
             HorizontalAlignment="Center"
             VerticalScrollBarVisibility="Auto"
             GotKeyboardFocus="TextBoxGotKeyboardFocus"
             LostKeyboardFocus="TextBoxLostKeyboardFocus"
             KeyDown="SourceTextKeyDown"/>
  </StackPanel>
</Border>
private void TextBoxGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    TextBox source = e.Source as TextBox;

    if (source != null)
    {
        // Change the TextBox color when it obtains focus.
        source.Background = Brushes.LightBlue;

        // Clear the TextBox.
        source.Clear();
    }
}
Private Sub TextBoxGotKeyboardFocus(ByVal sender As Object, ByVal e As KeyboardFocusChangedEventArgs)
	Dim source As TextBox = TryCast(e.Source, TextBox)

	If source IsNot Nothing Then
		' Change the TextBox color when it obtains focus.
		source.Background = Brushes.LightBlue

		' Clear the TextBox.
		source.Clear()
	End If
End Sub
private void TextBoxLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    TextBox source = e.Source as TextBox;

    if (source != null)
    {
        // Change the TextBox color when it loses focus.
        source.Background = Brushes.White;

        // Set the  hit counter back to zero and updates the display.
        this.ResetCounter();
    }
}
Private Sub TextBoxLostKeyboardFocus(ByVal sender As Object, ByVal e As KeyboardFocusChangedEventArgs)
	Dim source As TextBox = TryCast(e.Source, TextBox)

	If source IsNot Nothing Then
		' Change the TextBox color when it loses focus.
		source.Background = Brushes.White

		' Set the  hit counter back to zero and updates the display.
		Me.ResetCounter()
	End If
End Sub

注解

这是一个附加事件。This is an attached event. WPFWPF将附加事件作为路由事件实现。implements attached events as routed events. 附加事件本质上是XAMLXAML一种用于引用事件的语言概念, 这些事件可在未定义该事件的对象WPFWPF上进行处理, 后者通过同时启用事件来遍历路由。Attached events are fundamentally a XAMLXAML language concept for referencing events that can be handled on objects that do not define that event, which WPFWPF expands upon by also enabling the event to traverse a route. 附加事件在代码中没有直接处理语法;若要在代码中附加路由事件的处理程序, 请使用指定的 Add * 处理程序方法。Attached events do not have a direct handling syntax in code; to attach handlers for a routed event in code, you use a designated Add*Handler method. 有关详细信息, 请参阅附加事件概述For details, see Attached Events Overview.

键盘焦点指的是接收键盘输入的对象。Keyboard focus refers to the object that is receiving keyboard input. 具有键盘焦点的元素已IsKeyboardFocused设置为trueThe element with keyboard focus has IsKeyboardFocused set to true. 整个桌面上只能有一个具有键盘焦点的元素。There can be only one element with keyboard focus on the entire desktop. 逻辑焦点指焦点范围内的对象。Logical focus refers to the object within a focus scope that has focus. 有关焦点、键盘焦点和逻辑焦点的详细信息, 请参阅输入概述焦点概述For more information on focus, keyboard focus, and logical focus, see Input Overview and Focus Overview.

如果处理PreviewGotKeyboardFocus了事件PreviewLostKeyboardFocus或事件, 键盘焦点会更改。If the PreviewGotKeyboardFocus event or the PreviewLostKeyboardFocus event is handled, keyboard focus does change.

路由事件信息Routed Event Information

标识符字段Identifier field GotKeyboardFocusEvent
路由策略Routing strategy 冒泡Bubbling
委托Delegate KeyboardFocusChangedEventHandler

适用于