Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Global Style?

Tags:

Is there a way to setup global styles for my WPF application?

What I'm hoping to do is apply a style to all my Buttons that also have an Image child.

like image 342
Sonny Boy Avatar asked Aug 25 '10 20:08

Sonny Boy


People also ask

How do I style in WPF?

Template property, you can use a style to define or set a template. Designers generally allow you to create a copy of an existing template and modify it. For example, in the Visual Studio WPF designer, select a CheckBox control, and then right-click and select Edit template > Create a copy.

How do I apply multiple styles in WPF?

If you change the TargetType in the second style (in first set of xaml above) to ButtonBase , the two Styles do not get applied. However, check out the following xaml below to get around that restriction. Basically, it means you need to give the Style a key and reference it with that key.

What is Contentcontrol WPF?

Content Control is a base class that provides standardised functionality to WPF Controls. The Content Control class represents controls that can include a single item of content. This content is commonly plain text or a child control. Content Control is a subclass of the Control class in WPF.

What is a StackPanel WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.


1 Answers

Well, sort of - it's a catch-all approach you can do - put the following element in your App.xaml - all your buttons will change (except the ones you apply a style to, manually).

<Style TargetType="{x:Type Button}">     <Setter Property="Background" Value="LightPink"/> <!-- You should notice that one... --> </Style> 

However, if you want to hit only buttons with images - you have to inherit from Button everytime you do and then apply a style like this:

public class CustomImageButton:Button{} <Style TargetType="{x:Type local:CustomImageButton}">     <Setter Property="Background" Value="LimeGreen"/> </Style> <local:CustomImageButton Content="ClickMe"/> 

It is a very coarse-grained global styling - and you need to follow the convention to make it work.

An alternative is to use Themes - read more about that here.

like image 158
Goblin Avatar answered Sep 27 '22 19:09

Goblin