wpf 将应用程序级样式应用于所有文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14908148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:33:46  来源:igfitidea点击:

Apply an application-level style to all textboxes

wpfxamlstyles

提问by dotNET

How do I apply a style defined in my Application.xaml to all the textboxes in a particular window? I don't want to type Style="{StaticResource MyStyle}"with each and every of them because there are literally dozens of them. This is WPF + VS2010.

如何将 Application.xaml 中定义的样式应用于特定窗口中的所有文本框?我不想Style="{StaticResource MyStyle}"和每个人一起打字,因为实际上有几十个。这是 WPF + VS2010。

回答by sa_ddam213

Then just add the Styleto your App.Xamlor your Theme.xaml(if you have one) or even your Window.Resourcesif you just have 1 Window, just make sure you don't set the x:Key

然后只需添加Style到您的App.Xaml或您的Theme.xaml(如果您有)或什至您的,Window.Resources如果您只有 1 Window,只需确保您没有设置x:Key

Example:

例子:

This will apply to all TextBoxes(no x:Key)

这将适用于所有TextBoxes(无 x:Key)

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="Red" />
</Style>    

TextBoxes will have to use Style="{StaticResource MyStyle}"to use this :

必须使用 TextBoxesStyle="{StaticResource MyStyle}"来使用它:

<Style x:Key="MyStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="Red" />
</Style>