问题描述
如何将 Application.xaml 中定义的样式应用于特定窗口中的所有文本框?我不想在其中输入 Style="{StaticResource MyStyle}"
,因为实际上有几十个.这是 WPF + VS2010.
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.
推荐答案
然后只需将 Style
添加到您的 App.Xaml
或您的 Theme.xaml
(如果你有的话)甚至你的 Window.Resources
如果你只有 1 个 Window
,只要确保你没有设置 x:密钥
Then just add the Style
to your App.Xaml
or your Theme.xaml
(if you have one) or even your Window.Resources
if you just have 1 Window
, just make sure you don't set the x:Key
示例:
这将适用于所有 TextBoxes
(无 x:Key)
This will apply to all TextBoxes
(no x:Key)
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Red" />
</Style>
TextBoxes 必须使用 Style="{StaticResource MyStyle}"
来使用它:
TextBoxes will have to use Style="{StaticResource MyStyle}"
to use this :
<Style x:Key="MyStyle" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Red" />
</Style>
这篇关于将应用程序级样式应用于所有文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!