WPF - 全球风格? [英] WPF - Global Style?

查看:24
本文介绍了WPF - 全球风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为我的 WPF 应用程序设置全局样式?

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

我希望做的是将样式应用于所有也有 Image 子项的按钮.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆