WPF - 如何启用 TextFormattingMode=“显示”对于应用程序中的所有控件?
我目前正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该 Style 将仅应用于 ContentControl 类型的控件,而不会应用于从 ContentControl 派生的类型(即 Button、Window 等)。这就是隐式样式的工作原理。
如果您将该样式放入 Application.Resources 中,那么它将应用于应用程序中的每个 ContentControl,无论该控件位于哪个窗口中。如果您在特定窗口的资源中定义它,那么它只会被应用到该窗口中的 ContentControls。
TextOptions.TextFormattingMode 属性是继承的,这意味着您只需需要将其设置在可视化树的顶部。因此,如果放在 Application.Resources 中,这样的东西应该可以工作:
编辑:
或者您可以通过覆盖默认值将其应用于所有 Windows,甚至派生类型,如下所示:
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:
EDIT:
Or you could apply this to all Windows, even derived types, by overriding the default value like so: