style文件的指定
2024-08-26 22:00:17
新建资源文件
写资源文件
<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
最新文章
- 【XLL 框架库函数】 TempActiveRow/TempActiveRow12
- 实现password框中显示文字提示的方式
- TimeQuest学习
- modesim测试语句
- NOIP2013 题解
- github student pack中的digital ocean可以使用银联卡支付
- json_decode和json_encode
- 数据库类II
- react-native 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
- 监控Nginx负载均衡器脚本
- git warning: LF will be replaced by CRLF in 解决办法
- poj 3158kickdown
- Linux安装完Tomcat后无法登陆管理界面
- NFTS数据流
- Python:怎样用线程将任务并行化?
- 游戏AI之决策结构—有限状态机/行为树(2)
- 从.Net到Java学习第十篇——Spring Boot文件上传和下载
- 解题(DirGraCheckPath--有向图的遍历(深度搜索))
- django之paginator
- 使用webpack打包js文件(隔行变色案例)
热门文章
- Java CPU使用率过高分析
- hadoop集群ambari搭建(1)之ambari-server安装
- jquery-2 jQuery原理和核心方法(多看学习视频)
- [Typescript] What is a Function Type ? Function Types and Interfaces - Are They Related ?
- HTML中DOM对象的属性和方法的层级关系是怎样的?(目录即层次)
- Android 实现Xmpp工具类
- Path类的最全面具体解释 - 自己定义View应用系列
- 利用spingmvc及servlet实现对url的地址去除后缀,更改后缀为html
- Gson的使用(JsonObject)
- 学习鸟哥的Linux私房菜笔记(12)——系统监视2