Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply an application-level style to all textboxes

Tags:

styles

wpf

xaml

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.

like image 682
dotNET Avatar asked Feb 16 '13 07:02

dotNET


People also ask

How do you use style in XAML?

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.

What is style in WPF?

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.


1 Answers

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>     
like image 137
sa_ddam213 Avatar answered Sep 21 '22 13:09

sa_ddam213