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

  • 依赖属性由特定的派生类继承,但派生类会重写依赖属性的元数据并更改属性值。
  • 样式或模板应用于元素,该元素以不同的方式设置依赖属性值。

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

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

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

Dependency 属性信息

项目 价值
标识符字段 FocusableProperty
元数据属性设置为 true None

继承者说明

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

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

myElement 应为重写元数据值的类型的类名。

适用于

另请参阅