WPF定义样式文件的方式

场景:一个页面中有两类按钮,分别为样式A和样式B,但是WPF中不能像Web一样定义多个样式

样式定义方法:

1. 一个一个写内联样式 

2. 定义样式<style TargetType="Button"> ,只要引用了资源字典,所有button样式都会改变,缺点是不能定义多种样式

3. 定义样式<style x:key="xxx" TargetType="Button">,然后在Button style="{staticresource xxx}"或者style="{dynamicresource xxx}",缺点是每个Buttond都要写一次

4. 添加用户控件ButtonA, ButtonB,都继承Button,然后定义<style TargetType="ButtonStyleA">, <style TargetType="ButtonStyleB">,这样可以对A和B分别设置样式

样式引用方法:

1. 写在同一文件的资源字典中

2. 写在app.xaml中

3. 写在资源文件中,然后在需要的文件中引用该资源字典

ps:样式 <style x:key="xxx" TargetType="button">如果不写key值,会对所有button生效,而且后面定义的样式会覆盖前面的样式。

原文地址:https://www.cnblogs.com/bincoding/p/8359622.html