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.
The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources. Put simply, where you declare a style affects where the style can be applied.
Styles provide us the flexibility to set some properties of an object and reuse these specific settings across multiple objects for a consistent look. In styles, you can set only the existing properties of an object such as Height, Width, Font size, etc. Only default behavior of a control can be specified.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With