WPF - 如何启用 TextFormattingMode=“显示”对于应用程序中的所有控件?

发布于 2024-10-24 12:30:41 字数 410 浏览 9 评论 0原文

我目前正在 Windows XP 上进行 WPF 编程,其中抗锯齿效果呈现为模糊文本。我们希望通过将 TextOptions.TextFormattingMode 设置为 Display 来在整个 WPF 应用程序上消除锯齿。下面的代码解决了所有用户控件及其所有内容的问题,但没有解决我们从应用程序打开的窗口的问题。我应该在 TargetType 中设置什么类型来覆盖应用程序中的所有窗口和用户控件元素?或者有更好的方法来做到这一点吗?

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-10-31 12:30:41

该 Style 将仅应用于 ContentControl 类型的控件,而不会应用于从 ContentControl 派生的类型(即 Button、Window 等)。这就是隐式样式的工作原理。

如果您将该样式放入 Application.Resources 中,那么它将应用于应用程序中的每个 ContentControl,无论该控件位于哪个窗口中。如果您在特定窗口的资源中定义它,那么它只会被应用到该窗口中的 ContentControls。

TextOptions.TextFormattingMode 属性是继承的,这意味着您只需需要将其设置在可视化树的顶部。因此,如果放在 Application.Resources 中,这样的东西应该可以工作:

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

编辑:

或者您可以通过覆盖默认值将其应用于所有 Windows,甚至派生类型,如下所示:

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));
        }
    }
}

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));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文