Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
UIElement Class
 Focusable Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
UIElement..::.Focusable Property

Gets or sets a value that indicates whether the element can receive focus. This is a dependency property.

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation
Visual Basic (Declaration)
Public Property Focusable As Boolean
Visual Basic (Usage)
Dim instance As UIElement
Dim value As Boolean

value = instance.Focusable

instance.Focusable = value
C#
public bool Focusable { get; set; }
Visual C++
public:
virtual property bool Focusable {
    bool get () sealed;
    void set (bool value) sealed;
}
JScript
public final function get Focusable () : boolean
public final function set Focusable (value : boolean)
XAML Attribute Usage
<object Focusable="bool" .../>

Property Value

Type: System..::.Boolean
true if the element is focusable; otherwise false. The default is false, but see Remarks.

Implements

IInputElement..::.Focusable

Identifier field

FocusableProperty

Metadata properties set to true

None

Only the focused element receives keyboard input.

Focusable is the Microsoft .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.

For example, the apparent "default" of Focusable for a Button control will be true, even though Button inherits Focusable as a common language runtime (CLR) property directly from 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.

When inherited by Control or its derived classes, Control redefines the default value of this property to be true.

When inherited by Label (which is a Control derived class), the default value is again redefined to be false.

Notes to Inheritors:

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:

C#
FocusableProperty.OverrideMetadata(typeof(myElement), new UIPropertyMetadata(true));

where myElement should be the class name of the type that you are overriding the metadata value on.

The 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>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker