WPF-全球风格? [英] WPF - Global Style?
问题描述
是否可以为WPF应用程序设置全局样式?
Is there a way to setup global styles for my WPF application?
我希望做的是将样式应用于我所有具有Image子代的Button.
What I'm hoping to do is apply a style to all my Buttons that also have an Image child.
推荐答案
嗯,-您可以做的一种万能的方法-将以下元素放入App.xaml-您的所有按钮都会改变(除了手动应用样式的样式.
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>
但是,如果您只想单击带有图像的按钮,则每次操作都必须继承Button,然后应用如下样式:
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.
这篇关于WPF-全球风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!