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 プロパティ アクセサーです。 この特定の依存関係プロパティは、派生要素クラス (特にコントロール) では、明らかな "既定" の値が異なる設定を持つ場合がよくあります。 これは一般的に、次の 2 つの方法のいずれかで発生します。

  • 依存関係プロパティは特定の派生クラスによって継承されますが、その派生クラスは依存関係プロパティのメタデータをオーバーライドし、プロパティの既定値を変更します。
  • スタイルまたはテンプレートが要素に適用され、依存関係プロパティの値が異なる方法で設定されます。

たとえば、Buttonが共通言語ランタイム (CLR) プロパティとしてFocusableUIElementから直接継承している場合でも、Button コントロールのFocusableの明らかな "既定値" はtrueされます。 これは、Focusable依存関係プロパティの適用されたメタデータ値が、クラス階層内のButtonUIElementの間にある、Control基底クラスの静的コンストラクター内でオーバーライドされたためです。

Controlまたはその派生クラスによって継承Control、このプロパティの既定値を再定義してtrue

Label (Control 派生クラス) によって継承された場合、既定値は再び再定義され、falseされます。

依存関係プロパティ情報

品目 価値
識別子フィールド FocusableProperty
に設定されたメタデータ プロパティ true None

注意 (継承者)

(Controlではなく) UIElementから直接派生する場合は、既定では要素がフォーカス可能ではないため、要素をフォーカス可能にするかどうかを検討します。 要素をフォーカス可能にする場合は、次のように型の静的コンストラクター内でこのプロパティのメタデータをオーバーライドします。

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

myElement は、メタデータ値をオーバーライドする型のクラス名である必要があります。

適用対象

こちらもご覧ください