新建资源文件

 

写资源文件

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfDemo.Style">
<Style TargetType="Button" x:Key="MyStyleButton">
<Setter Property="Background" Value="Pink"></Setter>
</Style>
</ResourceDictionary>

引用资源

在独立的窗口/用户控件中引用

<Window x:Class="WpfDemo.Style.UseResourceDictionaryDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDemo.Style"
mc:Ignorable="d"
Title="UseResourceDictionaryDemo" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyStyle.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Style="{StaticResource MyStyleButton}"></Button>
</Grid>
</Window>

在App.xaml中统一引用(全局引用)

<!--App.xaml(WPF原生默认style.xaml,不同框架有所不同)-->
<Application x:Class="MAS.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/MyStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

小结

写法和写在Resources中一样,只是多了个ResourceDictionary的文件,并需要引用ResourceDictionary文件才能使用Style

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Style 下的MyStyle和UseResourceDictionaryDemo

最新文章

  1. 【XLL 框架库函数】 TempActiveRow/TempActiveRow12
  2. 实现password框中显示文字提示的方式
  3. TimeQuest学习
  4. modesim测试语句
  5. NOIP2013 题解
  6. github student pack中的digital ocean可以使用银联卡支付
  7. json_decode和json_encode
  8. 数据库类II
  9. react-native 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
  10. 监控Nginx负载均衡器脚本
  11. git warning: LF will be replaced by CRLF in 解决办法
  12. poj 3158kickdown
  13. Linux安装完Tomcat后无法登陆管理界面
  14. NFTS数据流
  15. Python:怎样用线程将任务并行化?
  16. 游戏AI之决策结构—有限状态机/行为树(2)
  17. 从.Net到Java学习第十篇——Spring Boot文件上传和下载
  18. 解题(DirGraCheckPath--有向图的遍历(深度搜索))
  19. django之paginator
  20. 使用webpack打包js文件(隔行变色案例)

热门文章

  1. Java CPU使用率过高分析
  2. hadoop集群ambari搭建(1)之ambari-server安装
  3. jquery-2 jQuery原理和核心方法(多看学习视频)
  4. [Typescript] What is a Function Type ? Function Types and Interfaces - Are They Related ?
  5. HTML中DOM对象的属性和方法的层级关系是怎样的?(目录即层次)
  6. Android 实现Xmpp工具类
  7. Path类的最全面具体解释 - 自己定义View应用系列
  8. 利用spingmvc及servlet实现对url的地址去除后缀,更改后缀为html
  9. Gson的使用(JsonObject)
  10. 学习鸟哥的Linux私房菜笔记(12)——系统监视2