UIElement.Focusable Property


获取或设置一个值,该值指示元素能否得到焦点。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

Property Value


如果元素能得到焦点,则为 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.

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.

例如,Button 控件的 Focusable 的外观 "默认" 将 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 将此属性的默认值重定义为 "true"。When 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

Notes to Inheritors

当直接从 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.

Applies to

See also