UIElement.IsFocused 屬性


取得值,決定這個項目是否具有邏輯焦點。Gets a value that determines whether this element has logical focus. 這是相依性屬性。This is a dependency property.

 property bool IsFocused { bool get(); };
public bool IsFocused { get; }
member this.IsFocused : bool
Public ReadOnly Property IsFocused As Boolean


如果這個項目具有邏輯焦點,則為 true;否則為 falsetrue if this element has logical focus; otherwise, false.


下列範例是程式碼處理常式,會在控制項焦點時變更其背景。The following example is a code handler that changes the background of a control when it is focused.

private void setColors(object sender, RoutedEventArgs e)
    if (myTextBox.IsFocused) myTextBox.Foreground = Brushes.Brown;

另一個達到相同效果的常見方式,是將 Trigger 放在控制項的 Style 中;這種方法不需要處理具有程式碼後置的特定事件,並可讓設計人員更容易存取 UIUI的互動和圖形化本質。Another common way to achieve this same effect is to place a Trigger in the Style of a control; this approach does not require handling specific events with code-behind and allows designers greater access to the interactive and graphical nature of the UIUI. 如需範例,請參閱如何:建立外部光亮效果For an example, see How to: Create an Outer Glow Effect.


如果應用程式有多個焦點區域(例如,功能表內容與應用程式的其餘部分),則邏輯焦點可能會與鍵盤焦點不同。Logical focus might differ from keyboard focus if an application has multiple focus divisions, such as between menu contents and the remainder of the application. 在此案例中,鍵盤焦點只能在應用程式 UI 的一個元素上,不過,其他焦點部門中的某些專案可能仍會保有邏輯焦點。In this scenario, keyboard focus can only be on one element of the application UI, however, certain elements in other focus divisions might still retain logical focus. 如需邏輯焦點的詳細資訊,請參閱焦點總覽For more information on logical focus, see Focus Overview.

您不會設定這個屬性(它是唯讀的)來設定焦點。You do not set focus by setting this property (it is read-only). 這個屬性的典型用法是使用它做為 SetterEventTrigger的相依性屬性。The typical use for this property is to use it as a dependency property for a Setter or EventTrigger. 若要以程式設計方式設定焦點,請呼叫 FocusTo set focus programmatically, call Focus. 焦點也可以由使用者動作或控制執行方式設定,這可能包括滑鼠捕捉行為。Focus can also be set by user action or by control implementations, which possibly include mouse capture behavior.

相依性屬性資訊Dependency Property Information

識別碼欄位Identifier field IsFocusedProperty
設定為 true 的中繼資料屬性Metadata properties set to true None