UIElement.IsFocused UIElement.IsFocused UIElement.IsFocused UIElement.IsFocused Property

定義

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

public:
 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;
}

若要達成此相同的效果的另一個常見方法是將放TriggerStyle控制項之這種方法不需要處理程式碼後置的特定事件,而且可讓設計工具的互動式和圖形化性質更大的存取UIUIAnother 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). 這個屬性的一般用法是做為相依性屬性SetterEventTriggerThe 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
若要設定的中繼資料屬性 trueMetadata properties set to true None

適用於

另請參閱