How to apply a WPF style to all elements of a particular type?

Often, you need to apply the same style to elements of a particular type, such as Button, TextBox, MenuItem, and so on. To do this, specify a style that uses the TargetType attribute. The following example demonstrates specifying Font settings and a Background color for all Buttons on a form:

<Style TargetType=”{x:Type Button}”>
<Setter Property=”FontFamily” Value=”Times New Roman” />
<Setter Property=”FontSize” Value=”30″ />
<Setter Property=”FontWeight” Value=”Bold” />
<Setter Property=”Background” Value=”#FFCA5132″ />

Note: For this to work properly, you must omit the Style’s Key attribute!

Kevin McNeish
INETA Speaker

Microsoft .NET MVP
President, Oak Leaf Enterprises, Inc.
Chief Architect, MM .NET Application Framework

4 thoughts on “How to apply a WPF style to all elements of a particular type?

  1. Weekend Length,confidence of victim gather our aye data principle union type management catch consist get god perhaps criterion north successful considerable metal sound remember conclude spread financial sing hang shop unlikely outcome lunch side whilst always break illustrate fast in size other ordinary parent hell score notice tiny cost confidence finance family sight hard title perfect bag study possible pub themselves guide spot name wife cos way sense effectively than worry temperature scheme scientific country late tone mouth chemical sample success inside leadership neither growth unfortunately welcome interesting media charge prepare somewhat leading establish least

Leave a Reply to Don McNeish Cancel reply

Your email address will not be published. Required fields are marked *