--- title: "Focus Overview" description: "Learn about the two main concepts in Windows Presentation Foundation that pertain to focus: keyboard focus and logical focus." ms.date: "03/30/2017" ms.topic: overview dev_langs: - "csharp" - "vb" helpviewer_keywords: - "applications [WPF], focus" - "focus in applications [WPF]" ms.assetid: 0230c4eb-0c8a-462b-ac4b-ae3e511659f4 --- # Focus Overview In [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] there are two main concepts that pertain to focus: keyboard focus and logical focus. Keyboard focus refers to the element that receives keyboard input and logical focus refers to the element in a focus scope that has focus. These concepts are discussed in detail in this overview. Understanding the difference in these concepts is important for creating complex applications that have multiple regions where focus can be obtained. The major classes that participate in focus management are the class, the class, and the base element classes, such as and . For more information about the base elements, see the [Base Elements Overview](base-elements-overview.md). The class is concerned primarily with keyboard focus and the is concerned primarily with logical focus, but this is not an absolute distinction. An element that has keyboard focus will also have logical focus, but an element that has logical focus does not necessarily have keyboard focus. This is apparent when you use the class to set the element that has keyboard focus, for it also sets logical focus on the element. ## Keyboard Focus Keyboard focus refers to the element that is currently receiving keyboard input. There can be only one element on the whole desktop that has keyboard focus. In [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)], the element that has keyboard focus will have set to `true`. The static property on the class gets the element that currently has keyboard focus. In order for an element to obtain keyboard focus, the and the properties on the base elements must be set to `true`. Some classes, such as the base class, have set to `false` by default; therefore, you must set to `true` if you want such an element to be able to obtain keyboard focus. Keyboard focus can be obtained through user interaction with the [!INCLUDE[TLA2#tla_ui](../../../includes/tla2sharptla-ui-md.md)], such as tabbing to an element or clicking the mouse on certain elements. Keyboard focus can also be obtained programmatically by using the method on the class. The method attempts to give the specified element keyboard focus. The returned element is the element that has keyboard focus, which might be a different element than requested if either the old or new focus object block the request. The following example uses the method to set keyboard focus on a . [!code-csharp[focussample#FocusSampleSetFocus](~/samples/snippets/csharp/VS_Snippets_Wpf/FocusSample/CSharp/Window1.xaml.cs#focussamplesetfocus)] [!code-vb[focussample#FocusSampleSetFocus](~/samples/snippets/visualbasic/VS_Snippets_Wpf/FocusSample/visualbasic/window1.xaml.vb#focussamplesetfocus)] The property on the base element classes gets a value indicating whether the element has keyboard focus. The property on the base element classes gets a value indicating whether the element or any one of its visual child elements has keyboard focus. When setting initial focus at application startup, the element to receive focus must be in the visual tree of the initial window loaded by the application, and the element must have and set to `true`. The recommended place to set initial focus is in the event handler. A callback can also be used by calling or . ## Logical Focus Logical focus refers to the in a focus scope. A focus scope is an element that keeps track of the within its scope. When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope. There can be multiple elements that have logical focus in an application, but there may only be one element that has logical focus in a particular focus scope. An element that has keyboard focus has logical focus for the focus scope it belongs to. An element can be turned into a focus scope in [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)] by setting the attached property to `true`. In code, an element can be turned into a focus scope by calling . The following example makes a into a focus scope by setting the attached property. [!code-xaml[MarkupSnippets#MarkupIsFocusScopeXAML](~/samples/snippets/csharp/VS_Snippets_Wpf/MarkupSnippets/CSharp/Window1.xaml#markupisfocusscopexaml)] [!code-csharp[FocusSnippets#FocusSetIsFocusScope](~/samples/snippets/csharp/VS_Snippets_Wpf/FocusSnippets/CSharp/Window1.xaml.cs#focussetisfocusscope)] [!code-vb[FocusSnippets#FocusSetIsFocusScope](~/samples/snippets/visualbasic/VS_Snippets_Wpf/FocusSnippets/visualbasic/window1.xaml.vb#focussetisfocusscope)] returns the focus scope for the specified element. Classes in [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] which are focus scopes by default are , , , and . gets the focused element for the specified focus scope. sets the focused element in the specified focus scope. is typically used to set the initial focused element. The following example sets the focused element on a focus scope and gets the focused element of a focus scope. [!code-csharp[FocusSnippets#FocusGetSetFocusedElement](~/samples/snippets/csharp/VS_Snippets_Wpf/FocusSnippets/CSharp/Window1.xaml.cs#focusgetsetfocusedelement)] [!code-vb[FocusSnippets#FocusGetSetFocusedElement](~/samples/snippets/visualbasic/VS_Snippets_Wpf/FocusSnippets/visualbasic/window1.xaml.vb#focusgetsetfocusedelement)] ## Keyboard Navigation The class is responsible for implementing default keyboard focus navigation when one of the navigation keys is pressed. The navigation keys are: TAB, SHIFT+TAB, CTRL+TAB, CTRL+SHIFT+TAB, UPARROW, DOWNARROW, LEFTARROW, and RIGHTARROW keys. The navigation behavior of a navigation container can be changed by setting the attached properties , , and . These properties are of type and the possible values are , , , , , and . The default value is , which means the element is not a navigation container. The following example creates a with a number of objects. The attached property is set to on the . When focus is changed using the tab key within the , focus will move from each element and when the last element is reached focus will return to the first element. [!code-xaml[MarkupSnippets#MarkupKeyboardNavigationTabNavigationXAML](~/samples/snippets/csharp/VS_Snippets_Wpf/MarkupSnippets/CSharp/Window1.xaml#markupkeyboardnavigationtabnavigationxaml)] [!code-csharp[MarkupSnippets#MarkupKeyboardNavigationTabNavigationCODE](~/samples/snippets/csharp/VS_Snippets_Wpf/MarkupSnippets/CSharp/Window1.xaml.cs#markupkeyboardnavigationtabnavigationcode)] [!code-vb[MarkupSnippets#MarkupKeyboardNavigationTabNavigationCODE](~/samples/snippets/visualbasic/VS_Snippets_Wpf/MarkupSnippets/visualbasic/window1.xaml.vb#markupkeyboardnavigationtabnavigationcode)] ## Navigating Focus Programmatically Additional API to work with focus are and . changes focus to the next element in the application. A is used to specify the direction. The passed to specifies the different directions focus can be moved, such as , , and . The following example uses to change the focused element. [!code-csharp[focussample#FocusSampleMoveFocus](~/samples/snippets/csharp/VS_Snippets_Wpf/FocusSample/CSharp/Window1.xaml.cs#focussamplemovefocus)] [!code-vb[focussample#FocusSampleMoveFocus](~/samples/snippets/visualbasic/VS_Snippets_Wpf/FocusSample/visualbasic/window1.xaml.vb#focussamplemovefocus)] returns the object which would receive focus if focus were to be changed. Currently, only , , , and are supported by . ## Focus Events The events related to keyboard focus are , and , . The events are defined as attached events on the class, but are more readily accessible as equivalent routed events on the base element classes. For more information about events, see the [Routed Events Overview](routed-events-overview.md). is raised when the element obtains keyboard focus. is raised when the element loses keyboard focus. If the event or the event is handled and is set to `true`, then focus will not change. The following example attaches and event handlers to a . [!code-xaml[keyboardsample#KeyboardSampleXAMLHandlerHookup](~/samples/snippets/csharp/VS_Snippets_Wpf/KeyboardSample/CSharp/Window1.xaml#keyboardsamplexamlhandlerhookup)] When the obtains keyboard focus, the property of the is changed to . [!code-csharp[keyboardsample#KeyboardSampleGotFocus](~/samples/snippets/csharp/VS_Snippets_Wpf/KeyboardSample/CSharp/Window1.xaml.cs#keyboardsamplegotfocus)] [!code-vb[keyboardsample#KeyboardSampleGotFocus](~/samples/snippets/visualbasic/VS_Snippets_Wpf/KeyboardSample/visualbasic/window1.xaml.vb#keyboardsamplegotfocus)] When the loses keyboard focus, the property of the is changed back to white. [!code-csharp[keyboardsample#KeyboardSampleLostFocus](~/samples/snippets/csharp/VS_Snippets_Wpf/KeyboardSample/CSharp/Window1.xaml.cs#keyboardsamplelostfocus)] [!code-vb[keyboardsample#KeyboardSampleLostFocus](~/samples/snippets/visualbasic/VS_Snippets_Wpf/KeyboardSample/visualbasic/window1.xaml.vb#keyboardsamplelostfocus)] The events related to logical focus are and . These events are defined on the as attached events, but the does not expose CLR event wrappers. and expose these events more conveniently. ## See also - - - - [Input Overview](input-overview.md) - [Base Elements Overview](base-elements-overview.md)