第一种方式:瞄准控件的基类

如下例所示,继承ContentControl的控件,都可以使用这个Style

<Window.Resources>
<Style x:Key="ContentControlStyle" TargetType="ContentControl">
<Setter Property="Background" Value="Pink"></Setter>
<Setter Property="FontSize" Value="12"></Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label Style="{StaticResource ContentControlStyle}" Content="按钮和文本框使用了基于ContentControl的Style"></Label>
<Button Style="{StaticResource ContentControlStyle}" Content="按钮" Grid.Row="1"></Button>
</Grid>

第二种方式:继承

<Window.Resources>
<Style x:Key="baseStyle" TargetType="Button">
<Setter Property="Background" Value="Pink"></Setter>
<Setter Property="FontSize" Value="12"></Setter>
</Style>
<Style x:Key="childStyle" TargetType="Button" BasedOn="{StaticResource baseStyle}">
<Setter Property="FontSize" Value="50"></Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource childStyle}" Content="按钮"></Button>
</Grid>

就近原则:父类子类同时设定属性,调用子类,以子类为准

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Style 的StyleInheritDemo和StyleInheritDemo2

最新文章

  1. 纯CSS3实现多层云彩变换飞行动画
  2. 跳转页面,传递参数——android
  3. BZOJ2981 : [Poi2002]括号
  4. C# gridControl 部分设置
  5. U盘格式化后的恢复
  6. 转: SQL Server索引的维护 - 索引碎片、填充因子
  7. java jdbc----mysql的select、insert、update、delete
  8. 逻辑网络(Logical Network)
  9. android studio常用快捷键(不断补充)
  10. TreeView控件例子
  11. css Tab选项卡1
  12. 【转】flash air中读取本地文件的三种方法
  13. 从编译器角度理解C++中的引用和指针
  14. javascript templating
  15. POJ 2411 Mondriaan&#39;s Dream -- 状压DP
  16. [物理学与PDEs]第1章习题11 各向同性导体中电荷分布的指数衰减
  17. [CodeForce 450A] Jzzhu and Children
  18. AI Haar特征
  19. mysql学习笔记--数据库操作
  20. 通过sql语句修改表的结构

热门文章

  1. 解决Eclipse中文乱码 分类: B1_JAVA 2014-06-25 11:23 336人阅读 评论(0) 收藏
  2. 【C#】万事开头难(二)&amp;lt;机房重构&amp;gt;
  3. Android Error:(1,N1) 错误: 需要class, interface或enum
  4. php实现 提取不重复的整数(编程题目能够最快的熟悉函数)
  5. js进阶 11-7 jquery如何获取和改变元素的位置
  6. 数据库使用char要留心
  7. 学习游戏服务器开发必看,C++游戏服务器开发常用工具介绍
  8. Wpf的布局舍入属性(可以解决软件字体模糊的问题)
  9. 探险 - 树型dp(背包)/多叉树转二叉树
  10. Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)