Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xaml - WPF - Global Style?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...