Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FrameworkElement.FocusVisualStyle Property

Gets or sets a property that enables customization of appearance, effects, or other style characteristics that will apply to this element when it captures keyboard focus. This is a dependency property.

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public Style FocusVisualStyle { get; set; }
/** @property */
public Style get_FocusVisualStyle ()

/** @property */
public void set_FocusVisualStyle (Style value)

public function get FocusVisualStyle () : Style

public function set FocusVisualStyle (value : Style)

<object FocusVisualStyle="{resourceExtension styleResourceKey}"/>
 resourceExtension  One of the following: StaticResource, or DynamicResource. See Resources Overview.  styleResourceKey  The key that identifies the style being requested. The key refers to an existing resource in a ResourceDictionary. 

Property Value

The desired style to apply on focus. The default value as declared in the dependency property is an empty static Style. However, the effective value at runtime is often (but not always) a style as supplied by theme support for controls.

Identifier field

FocusVisualStyleProperty

Metadata properties set to true

None

This property affects visual appearance but does not report AffectsRender in metadata. This is because the visual appearance change is event-driven and may not apply at all times, and therefore should not generally report any visual or layout information in metadata.

Conceptually, the visual behavior of focus applied to a control should be coherent from control to control. The most sensible way to enforce coherence is to only change the focus visual style if you are composing an entire theme. Each control that is defined in the theme will get either the very same style, or some variation of a style that at least is visually coherent from control to control. Setting this property on invididual control styles and not as part of a theme is not the intended usage of this property, because it may lead to a confusing user experience regarding keyboard focus. If you are intending control-specific behavior that is deliberately not coherent across a theme, a much better approach is to use triggers in styles for individual input state properties, such as IsFocused or IsKeyboardFocusWithin, and to do so in a way that does not visually interfere with any existing focus visual style.

This example shows you how to create a focus-specific style in resources that is applied to the control, using the FocusVisualStyle property.

The following example defines a style that creates additional control compositing that only applies when that control is focused in the user interface (UI). This is accomplished by defining a style with a ControlTemplate, then referencing that style as a resource when setting the FocusVisualStyle. Note that in this case the FocusVisualStyle is additive to any normal style; the fact that you create a new ControlTemplate does not impact the normal styling of the button, as it would had you attempted to create a style with a ControlTemplate and setting that style to the Style property. In this case an external rectangle resembling a border is placed outside of the rectangular area that is bound to the actual height and width of the rectangular control being focused. Negative values are set on Top and Left to make the border appear outside the focused control, somewhat like a highlight.

NoteNote:

Conceptually, the visual behavior of focus applied to a control should be coherent from control to control. The most sensible way to ensure coherence is to only change the focus visual style if you are composing an entire theme, where each control that is defined in the theme will get either the very same Style, or some variation of a Style that at least is visually coherent from control to control. Setting this property on invididual control styles and not as part of a theme is not the intended usage of this property because it may lead to a confusing user experience regarding keyboard focus. If you are intending control-specific behaviors that are deliberately not coherent across a theme, a much better approach is to use triggers in styles for individual input state properties, such as IsFocused or IsKeyboardFocusWithin.

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
  <Page.Resources>
    <Style x:Key="MyFocusVisual">
      <Setter Property="Control.Template">
        <Setter.Value>
          <ControlTemplate>
            <Rectangle Margin="-2"  StrokeThickness="1" Stroke="Red" StrokeDashArray="1 2"/>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Page.Resources>
  <StackPanel Background="Ivory" Orientation="Horizontal">
	  <Canvas Width="10"/>
	  <Button Width="100" Height="30" FocusVisualStyle="{DynamicResource MyFocusVisual}">Focus Here</Button>
	  <Canvas Width="100"/>
	  <Button Width="100" Height="30" FocusVisualStyle="{DynamicResource MyFocusVisual}">Focus Here</Button>
  </StackPanel>
</Page>

For the complete sample, see Creating a FocusVisualStyle Sample.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.