愤怒者系列(一) WPF引用样式文件
来源:互联网 发布:淘宝透明睡衣买家秀 编辑:程序博客网 时间:2024/05/16 05:58
今天我们说一下wpf引用样式资源文件。
wpf的样式控制分为行内,页内,和样式文件。而样式资源文件又分为程序内的样式文件和页内的样式文件。
我们今天讲的是引用的样式文件,因为可以切换样式文件来改变主题。
如果一个wpf程序同时有程序内的样式文件和页内文件,默认情况下是会匹配到页内样式文件的,同理,行内的优先级更高。
例子
例子里面我们程序内是引用Style.xaml,页面内是引用SimpleStyle.xaml来做比较。
创建样式文件
首先我们添加一个wpf的资源词典的xaml,我们改名字叫做Style.xaml。然后里面写上样式。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfStyleTest"> <Style TargetType="{x:Type Button}"> <Setter Property="Height" Value="25"></Setter> <Setter Property="Width" Value="200"></Setter> <Setter Property="Foreground" Value="#FF00FF23"/> <Setter Property="Background" Value="#FFBFAF0C"/> </Style> <Style x:Key="BStyle" TargetType="{x:Type Button}"> <Setter Property="Height" Value="25"></Setter> <Setter Property="Width" Value="200"></Setter> <Setter Property="Foreground" Value="#FFBFAF0C"/> <Setter Property="Background" Value="#FF00FF23"/> </Style></ResourceDictionary>
备注:这里的TargetType是用来匹配控件类型的,如果引入了样式文件就会控制这个类型的所有样式,如果有个别按钮需要特别样式,我们可以使用key来指定使用特别的样式。
下面是SimpleStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfStyleTest"> <Style x:Key="simBut" TargetType="{x:Type Button}"> <Setter Property="Height" Value="25"></Setter> <Setter Property="Width" Value="200"></Setter> <Setter Property="Foreground" Value="#FF00FF23"/> <Setter Property="Background" Value="Red"/> </Style></ResourceDictionary>
程序内引用
然后我们修改App.xaml引用资源文件
<Application x:Class="WpfStyleTest.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfStyleTest" StartupUri="MainWindow.xaml"> <Application.Resources> <!--引用外部资源--> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Style.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources></Application>
页面内引用
<Window x:Class="WpfStyleTest.MainWindow" 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:WpfStyleTest" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <!--引用外部资源--> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="SimpleStyle.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources></Window>备注:页面内引用和程序内引用是可以同时引用的,引用了两个不同的样式的情况下,默认是匹配页面内的样式引用,指定key是可以指定页面内和程序内的样式。
使用
<Window x:Class="WpfStyleTest.MainWindow" 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:WpfStyleTest" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <!--引用外部资源--> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="SimpleStyle.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <StackPanel Orientation="Vertical"> <Button x:Name="button" Style="{StaticResource simBut}" Content="窗口的外部资源,指定样式" /> <Button x:Name="button2" Style="{StaticResource BStyle}" Content="程序的外部资源,指定样式" /> <Button x:Name="button3" Content="程序的外部资源,默认样式" /> </StackPanel> </Grid></Window>
备注:指定key的是时候写上 Style="{StaticResource simBut}"对应的key就行了。
这里使用的时候如果加上一个页面的外部资源,默认样式的情况下,程序内的外部资源,默认样式就不起效果了。
1 0
- 愤怒者系列(一) WPF引用样式文件
- 愤怒者系列(二) WPF切换主题
- WPF 引用外部样式文件
- WPF自定义样式系列(一)——Button按钮样式
- WPF引用外部样式
- WPF引用外部样式
- wpf 引用资源文件
- WPF系列(一)依赖项属性
- WPF基础之样式设置和模板化(一)
- WPF自定义样式系列(二)——TextBox输入框样式(部分带水印)
- WPF :引用单独风格文件
- WPF 样式引用及样式存放地点--新手试炼
- WPF/E解读系列(一)
- xaml - 样式继承,引用样式文件
- WPF(三)举例讲解WPF样式
- WPF后台动态调用样式文件
- WPF 资源字典样式引用导致的DependencyProperty.UnsetValue
- WPF 中的样式(Style)
- UNIX 环境高级编程(二)—— linux共享库,/etc/ld.so.conf 及 ld.so.conf.d/libc.conf
- c++中引用传递与值传递的区别
- android蓝牙
- Android照片墙完整版,完美结合LruCache和DiskLruCache
- 第二天 iOS版本号
- 愤怒者系列(一) WPF引用样式文件
- Support and Release Notes
- 投硬币
- Android实现网络多线程断点续传下载(二)
- 代码质量管理
- Hibernate一对多关联
- GPS定位经纬度标准转换
- $.getjson()跨域问题,笔记
- TCP连接复用