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.

實作

範例

下列範例程式碼說明特定自訂控制項的控制項範本, 其會在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.

<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 .NETFocusable 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控制項的明顯「預設FocusableButton會是, 通用語言執行平台 (CLR)common language runtime (CLR) true即使直接Focusable繼承為屬性也是一樣。 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基類的靜態函式中覆寫, 而該類別Button位於UIElement類別階層架構的和之間。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 , 將這個屬性的預設值重新定義true為。When inherited by Control or its derived classes, Control redefines the default value of this property to be true.

當繼承者Label (這Control是衍生類別) 時, 預設值false會再次重新定義為。When 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 (相對於 from 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.

適用於

另請參閱