UIElement.Focusable UIElement.Focusable UIElement.Focusable UIElement.Focusable Property

定義

取得或設定元素是否能夠接收焦點的值。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

屬性值

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

實作

範例

下列範例程式碼說明特定的自訂控制項,它會設定控制項樣板Focusablefalse上其中一個範本內的項目。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.

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

比方說,明顯 「 預設 」 的FocusableforButton控制項將會是true,即使Button繼承Focusable作為通用語言執行平台 (CLR)common language runtime (CLR)屬性,直接從UIElementFor 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基底類別,之間平均分攤工作量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
若要設定的中繼資料屬性 trueMetadata 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.

適用於

另請參閱