Skip to main content
.NET Framework Class Library
UIElementIsFocused Property

Gets a value that determines whether this element has logical focus. This is a dependency property.

Namespace:   System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)
Syntax
Public ReadOnly Property IsFocused As [%$TOPIC/ms588703_en-us_VS_110_1_0_0_0_0%]
public [%$TOPIC/ms588703_en-us_VS_110_1_0_1_0_0%] IsFocused { get; }
public:
property [%$TOPIC/ms588703_en-us_VS_110_1_0_2_0_0%] IsFocused {
	[%$TOPIC/ms588703_en-us_VS_110_1_0_2_0_1%] get ();
}
member IsFocused : [%$TOPIC/ms588703_en-us_VS_110_1_0_3_0_0%] with get
function get IsFocused () : [%$TOPIC/ms588703_en-us_VS_110_1_0_4_0_0%]

Property Value

Type: SystemBoolean
true if this element has logical focus; otherwise, false.
Dependency Property Information

Identifier field

IsFocusedProperty

Metadata properties set to true

None

Remarks

Logical focus might differ from keyboard focus if an application has multiple focus divisions, such as between menu contents and the remainder of the application. In this scenario, keyboard focus can only be on one element of the application UI, however, certain elements in other focus divisions might still retain logical focus. For more information on logical focus, see Focus Overview.

You do not set focus by setting this property (it is read-only). The typical use for this property is to use it as a dependency property for a Setter or EventTrigger. To set focus programmatically, call Focus. Focus can also be set by user action or by control implementations, which possibly include mouse capture behavior.

Examples

The following example is a code handler that changes the background of a control when it is focused.

private void setColors(object sender, RoutedEventArgs e)
{
    if (myTextBox.IsFocused) myTextBox.Foreground = Brushes.Brown;
}

Another common way to achieve this same effect is to place a Trigger in the Style of a control; this approach does not require handling specific events with code-behind and allows designers greater access to the interactive and graphical nature of the UI. For an example, see How to: Create an Outer Glow Effect.

Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1