10

I am currently programming WPF on Windows XP where anti-aliasing is rendered as blury text. We want to anti-aliasing on the whole WPF application by setting TextOptions.TextFormattingMode to Display. The code below solves the issue for all user controls and all its content, but not for windows we open from the application. What type should I set in TargetType to cover all Window and User Control elements in the application? Or are there better ways to do this?

<Style TargetType="{x:Type ContentControl}">
     <Setter Property="TextOptions.TextFormattingMode" Value="Display"></Setter>
</Style>
chrisva
  • 625
  • 9
  • 17

1 Answers1

11

That Style will only be applied to controls of type ContentControl, it will not be applied to types that derive from ContentControl (i.e. Button, Window, etc). That's just how implicit Styles work.

If you put that Style in your Application.Resources, then it would be applied to every ContentControl in your application, regardless of what Window the control is in. If you define that in the Resouces of a specific Window, then it would only be applied to ContentControls in that Window.

The TextOptions.TextFormattingMode property is inherited, which means you only need to set it at the top of the visual tree. So something like this should work, if placed in the Application.Resources:

<Style TargetType="{x:Type Window}">
    <Setter Property="TextOptions.TextFormattingMode" Value="Display"></Setter>
</Style>

EDIT:

Or you could apply this to all Windows, even derived types, by overriding the default value like so:

using System.Windows;
using System.Windows.Media;

namespace MyProject
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application {
        static App() {
            TextOptions.TextFormattingModeProperty.OverrideMetadata(typeof(Window),
                new FrameworkPropertyMetadata(TextFormattingMode.Display, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits));
        }
    }
}
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • 2
    The style in this post actually does not work for the same reason that the ContentControl style does not work. The Window type in the style needs to be the specific class that your application uses for the window. (It is typically derived from Window.) At that point you might as well set the TextOptions property in the XAML for the Window itself since it will only work on that type anyways. Ideally there would be a solution that applies it to all windows reguardless of type so that it could cover things like pop-out windows and such. Any further ideas? – MrSlippers Jul 26 '11 at 20:49
  • @MrSlippers - Good point, I updated my answer with another option. – CodeNaked Jul 26 '11 at 20:55