UIElement.Focusable 屬性

定義

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

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

屬性值

Boolean

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

實作

範例

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

<Window.Resources>
  <Style x:Key="TextBoxNoScrollViewer" TargetType="{x:Type TextBoxBase}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBoxBase}">
          <Border 
            CornerRadius="2" 
            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.  
            -->
            <AdornerDecorator 
              x:Name="PART_ContentHost"
              Focusable="False" 
            />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</Window.Resources>

備註

只有焦點的元素會接收鍵盤輸入。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.

例如,控制項的明顯「預設」 Focusable Button 會是 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. 這是因為相依性屬性的已套用中繼資料值已在基類的靜態函式 Focusable 中覆寫,該 Control 類別位於類別階層架構的 Button 和之間 UIElementThis 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
中繼資料屬性設定為 trueMetadata properties set to true NoneNone

給繼承者的注意事項

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.

適用於

另請參閱