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

属性值

如果元素能得到焦点,则为 true;否则为 false。 默认值为 false

实现

示例

下面的示例代码演示了特定自定义控件的控件模板,该模板对模板中的某个元素进行设置 Focusablefalse

<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 属性访问器。 此特定依赖属性在派生元素类(特别是在控件中)中以不同方式设置其明显的“默认值”值。 这通常以以下两种方式之一发生:

  • 依赖属性由特定的派生类继承,但该派生类替代依赖属性的元数据并更改属性默认值。

  • 样式或模板应用于元素,该元素以不同的方式设置依赖属性值。

例如,控件的 明显“默认值”FocusableButton将为 true,即使 ButtonFocusable 作为公共语言运行时继承, (CLR) 属性直接从 UIElement继承。 这是因为在基类的静态构造函数Control(位于 类层次结构中的 和 UIElement 之间Button)中重写了依赖属性应用的元数据值Focusable

当由 Control 或其派生类继承时, Control 将此属性 true的默认值重新定义为 。

当由 Label (() Control 派生类)继承时,默认值再次重新定义为 false

依赖项属性信息

标识符字段 FocusableProperty
元数据属性设置为 true

继承者说明

当派生自 UIElement 直接 (而不是从 Control) 派生时,请考虑是否希望元素可聚焦,因为默认情况下元素将不可聚焦。 如果希望元素可聚焦,请在类型的静态构造函数中重写此属性的元数据,如下所示:

FocusableProperty.OverrideMetadata(typeof(myElement), new UIPropertyMetadata(true));
FocusableProperty.OverrideMetadata(GetType(myElement), New UIPropertyMetadata(True))

其中 myElement 应为要替代元数据值的类型的类名。

适用于

另请参阅