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.

例如Focusable , Focusable UIElement 公共语言运行时 (CLR)common language runtime (CLR) Button控件的外观 "默认"为,即使直接从中继承为属性。true ButtonFor 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是派生类) 时, 默认值将再次重定义为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.

适用于

另请参阅