資訊
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Keyboard.GotKeyboardFocus 附加事件

當元素接受鍵盤焦點時發生。

命名空間:  System.Windows.Input
組件:  PresentationCore (在 PresentationCore.dll 中)
針對 XAML 的 XMLNS: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

See AddGotKeyboardFocusHandler, RemoveGotKeyboardFocusHandler
<object Keyboard.GotKeyboardFocus="KeyboardFocusChangedEventHandler" .../>

識別項欄位

GotKeyboardFocusEvent

路由策略

反昇

委派

KeyboardFocusChangedEventHandler

這是附加事件。WPF實作附加事件與路由事件。 附加的事件基本上是XAML語言概念,來參考物件沒有定義該事件,就可以處理的事件而WPF會展開時,也啟用 [周遊路由事件。 附加的事件並沒有直接處理語法在 [程式碼路徑。 若要附加的程式碼中的路由事件處理常式,您可以使用指定的 Add * 處理常式方法。 如需詳細資訊,請參閱附加事件概觀

鍵盤焦點是指接收鍵盤輸入的物件。 具有鍵盤焦點的項目有IsKeyboardFocused設定為 [ true 可能會有鍵盤焦點在整個桌面上只能有一個項目。 邏輯焦點是指擁有焦點之焦點範圍內的物件。 如需有關焦點、 鍵盤焦點和邏輯焦點的詳細資訊,請參閱輸入概觀焦點概觀

如果PreviewGotKeyboardFocus事件或PreviewLostKeyboardFocus在處理事件,並不會變更鍵盤焦點。

下列範例會建立TextBox ,並將附加事件處理常式的GotKeyboardFocus事件與LostKeyboardFocus事件。 TextBox會取得鍵盤焦點變更背景色彩和文字的TextBox已清除。 TextBlock失去鍵盤焦點的背景色彩會變更,並會重設這個範例中使用變數來呼叫方法。


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



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();
    }
}


.NET Framework

支援版本:4.5、4、3.5、3.0

.NET Framework Client Profile

支援版本:4、3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色,不支援 Itanium)

此.NET Framework 並不支援各種平台的所有版本。如需支援版本的清單,請參閱.NET Framework 系統需求.

社群新增項目

顯示:
© 2014 Microsoft