Apply an application-level style to all textboxes

2019-01-25 03:20发布

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.

标签: wpf xaml styles
1条回答
爷、活的狠高调
2楼-- · 2019-01-25 03:58

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

Example:

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

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

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>    
查看更多
登录 后发表回答