UIElement.Focusable 屬性


取得或設定元素是否能夠接收焦點的值。Gets or sets a value that indicates whether the element can receive focus. 這是相依性屬性。This is a dependency property.

 property bool Focusable { bool get(); void set(bool value); };
public bool Focusable { get; set; }
member this.Focusable : bool with get, set
Public Property Focusable As Boolean


如果這個項目可設定焦點,則為 true;否則為 falsetrue if the element is focusable; otherwise false. 預設為 falseThe default is false.



下列範例程式碼說明特定自訂控制項的控制項範本,其會在範本內的其中一個元素上設定 Focusable falseThe following example code illustrates a control template for a particular custom control, which sets Focusable false on one of the elements within the template.

  <Style x:Key="TextBoxNoScrollViewer" TargetType="{x:Type TextBoxBase}">
    <Setter Property="Template">
        <ControlTemplate TargetType="{x:Type TextBoxBase}">
            Background="{TemplateBinding Background}" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            BorderBrush="{TemplateBinding BorderBrush}"  
            The control template for a TextBox or RichTextBox must
            include an element tagged as the content host.  An element is 
            tagged as the content host element when it has the special name
            PART_ContentHost.  The content host element must be a ScrollViewer,
            or an element that derives from Decorator.  


只有焦點的元素會接收鍵盤輸入。Only the focused element receives keyboard input.

Focusable 是相依性屬性實際的 Microsoft .NETMicrosoft .NET 屬性存取子。Focusable is the Microsoft .NETMicrosoft .NET property accessor for what is in reality a dependency property. 這個特定的相依性屬性經常會在衍生的元素類別中,以不同的方式設定其明顯的「預設」值,特別是在控制項中。This particular dependency property quite frequently has its apparent "default" value set differently in derived element classes, particularly in controls. 這通常會發生在下列兩種情況的其中一種:This commonly occurs in one of two ways:

  • 相依性屬性是由特定的衍生類別所繼承,但是該衍生類別會覆寫相依性屬性的中繼資料,並變更屬性的預設值。The dependency property is inherited by a particular derived class, but that derived class overrides the metadata of the dependency property and changes the property default value.

  • 樣式或範本會套用至專案,此專案會以不同的方式設定該相依性屬性的值。A style or template is applied to an element, which sets that dependency property value differently.

例如,Button 控制項的 Focusable 明顯「預設」將會 true,即使 Button 直接從 Focusable 繼承 通用語言執行平台 (CLR)common language runtime (CLR) 做為 UIElement屬性也一樣。For example, the apparent "default" of Focusable for a Button control will be true, even though Button inherits Focusable as a 通用語言執行平台 (CLR)common language runtime (CLR) property directly from UIElement. 這是因為在 Control 基類的靜態函式中,已覆寫 Focusable 相依性屬性的已套用中繼資料值,該類別位於類別階層中的 ButtonUIElement 之間。This is because the applied metadata value for the Focusable dependency property was overridden within the static constructor of the Control base class, which is situated between Button and UIElement in the class hierarchy.

Control 或其衍生類別繼承時,Control 重新定義此屬性的預設值為 trueWhen inherited by Control or its derived classes, Control redefines the default value of this property to be true.

當由 Label (這是 Control 的衍生類別)繼承時,會再次將預設值重新定義為 falseWhen inherited by Label (which is a Control derived class), the default value is again redefined to be false.

相依性屬性資訊Dependency Property Information

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


當直接從 UIElement 衍生(而不是從 Control)時,請考慮是否要讓元素成為可設定焦點,因為根據預設,專案將無法獲得焦點。When deriving from UIElement directly (as opposed to from Control), consider whether you wish your element to be focusable, because by default the element will not be focusable. 如果您想要讓元素成為可設定焦點,請在您類型的靜態函式中覆寫這個屬性的中繼資料,如下所示:If you wish your element to be focusable, override the metadata for this property within your type's static constructor as follows: [! code-csharpCorePseudocode # UIElementShortOverride][! code-vbCorePseudocode # UIElementShortOverride][!code-csharpCorePseudocode#UIElementShortOverride] [!code-vbCorePseudocode#UIElementShortOverride] 其中 myElement 應該是您要覆寫中繼資料值之類型的類別名稱。where myElement should be the class name of the type that you are overriding the metadata value on.