UIElement.Focusable 屬性

定義

取得或設定元素是否能夠接收焦點的值。 這是相依性屬性。

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;否則為 false。 預設為 false

實作

範例

下列範例程式碼說明特定自訂控制項的控制項範本,該控制項會在範本內的其中一個專案上設定 Focusable false

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

備註

只有焦點元素會接收鍵盤輸入。

Focusable 是實際相依性屬性的 Microsoft .NET 屬性存取子。 這個特定相依性屬性經常在衍生元素類別中以不同的方式設定其明顯的「預設值」值,特別是在控制項中。 這通常會以下列兩種方式之一發生:

  • 相依性屬性是由特定衍生類別繼承,但衍生類別會覆寫相依性屬性的中繼資料,並變更屬性預設值。

  • 樣式或範本會套用至元素,以不同的方式設定該相依性屬性值。

例如,控制項的明顯「預設」 Focusable 是 ,即使 Button 繼承 Focusable 為 Common Language Runtime (CLR) 屬性,仍會直接從 繼承 UIElementtrue Button 這是因為相依性屬性的套用中繼資料值 Focusable 是在基類的 Control 靜態建構函式內覆寫,而基類位於 類別階層中的 和 UIElement 之間 Button

由 或其衍生類別繼承 Control 時, Control 請將此屬性的預設值重新定義為 true

由衍生類別) (繼承 Label 時,預設值會再次重新定義為 falseControl

相依性屬性資訊

識別碼欄位 FocusableProperty
設定為 的中繼資料屬性 true

給繼承者的注意事項

UIElement 直接 (衍生,而不是從 Control) 衍生時,請考慮是否要讓元素成為可焦點,因為根據預設,元素將無法成為焦點。 如果您想要讓元素成為可焦點,請覆寫類型靜態建構函式內此屬性的中繼資料,如下所示: :::code language=「csharp」 source=「~/snippets/csharp/System。Windows/ComponentResourceKey/.ctor/corepseudocode.cs「 id=」Snippetuielementshortoverride「:::::code language=」vb「 source=」~/snippets/visualbasic/VS_Snippets_Wpf/CorePseudocode/visualbasic/corepseudocode.vb「 id=」Snippetuielementshortoverride「:: 其中 myElement 應該是您要覆寫中繼資料值的型別類別名稱。

適用於

另請參閱