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

dot*_*NET 23 wpf xaml styles

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

sa_*_*213 39

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

例:

这将适用于所有TextBoxes(没有x:键)

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="Red" />
</Style>    
Run Code Online (Sandbox Code Playgroud)

TextBoxes必须Style="{StaticResource MyStyle}"使用它:

<Style x:Key="MyStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="Red" />
</Style>    
Run Code Online (Sandbox Code Playgroud)

  • 这是你希望你没有问过的那些简单的事情之一.谢谢一堆. (3认同)