tags:

views:

1086

answers:

5

I want to globally deactivate the focus rectangles in my WPF application. For single controls that can be done via

<Style TargetType="Button">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>

but how to apply it to all controls in my application. When applying to FrameworkElement nothing happens. What I need would be something like "apply to class x and all derived classes".

Thanks in advance,

Stefan

A: 

This may not be simple, but you could write a function that alters the existing style of a control. Once that is written, you could write a function that recursively alters the style of each element.

John Fisher
Why did this get a -1?
John Fisher
Probably because it's unnecessarily complex, fraught with difficulties such as ensuring you've covered every edge case possible and being quite hard to maintain, just to name 2. Compared to a `Style` and `Setter` for each type (which, though tedious, is simple), your suggestion sounds insane. I didn't give the -1.
Joel B Fant
Since he hasn't made a comment that any of the solutions will work, we don't know whether the other ideas helped. If this is the only idea that meets his needs, "insane" would be an inaccurate depiction.
John Fisher
A: 

You can use OverrideMetadata:

FrameworkElement.FocusVisualStyleProperty.OverrideMetadata(
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(null));
  1. you have to call this before any element is created, the Application.Startup event is probably the best place.
  2. This will only effect controls that use FrameworkElement's focus visual style and will not change controls that override it in code or styles.
  3. I haven't tries doing this with FocusVisualStyle myself
Nir
Unfortunately this results in a "PropertyMetadata is already registered for type 'FrameworkElement'
grayscales
"Calls to OverrideMetadata should only be performed within the static constructors of the type that provides itself as the forType parameter of this method, or through similar instantiation." http://msdn.microsoft.com/en-us/library/ms597491.aspx
Joel B Fant
+1  A: 

I know it sounds tedious, but you'll probably have to do the same thing for all the other control types, individually. Making a list of them and doing a couple simple Find/Replace operations should get you what you need, though.

Joel B Fant
+1  A: 

Looks like there's no magic bullet for this:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/141c8bfa-f152-4f8a-aca0-3c3b5440d183

Andy
+1  A: 

According to http://msdn.microsoft.com/en-us/library/bb613567.aspx, you should be able to set global focus style like this:

<Style x:Key="{x:Static SystemParameters.FocusVisualStyleKey}">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate>
        <Rectangle StrokeThickness="1"
          Stroke="Black"
          StrokeDashArray="1 2"
          SnapsToDevicePixels="true"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style> 

I haven't tested it, but I guess that when you empty the controltemplate, this would effectively disable the focus rectangle for the whole app (provided you include this style in app.xaml).

Tomáš Kafka