Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - How to Enable TextFormattingMode="Display" for all controls in the application?

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>
like image 280
chrisva Avatar asked Mar 21 '11 11:03

chrisva


1 Answers

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));
        }
    }
}
like image 164
CodeNaked Avatar answered Oct 03 '22 05:10

CodeNaked