样式生效范围
根据生效范围从大到小依次为
- Application.Resources(所有页面生效)
- Window.Resources(当前页面生效)
- 组件内设置(当前组件生效)
Resources
1 | <Window.Resources> |
或者
1 | <Window.Resources> |
组件内设置
1 | <TextBlock Text="组件样式"> |
全部生效和引用生效
全部生效
1 | <Style TargetType="TextBlock"> |
注意
只要不设置
x:Key
,那么这个样式就会对所生效范围的所有组件生效,要只对某个生效就要设置x:Key
。全部生效的尽量不要设置太基础的组件,比如如果设置了
TextBlock
,所有的Button
的文字样式也会变化。
设置x:Key
后必须引用才能生效
1 | <Style x:Key="ZTextBlock" TargetType="TextBlock"> |
引用
1 | <TextBlock |
样式资源的合并
我们可以定义这样的文件StyleDefault.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
在使用的时候进行合并
OverwrideDefaultControlStyles.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:ZView"> |
Application中引用
1 | <Application |
样式和模板
设置样式的时候我们可以设置样式和模板。
1 | <Style |
自定义组件
项目下添加Resources
文件夹,添加以下文件
简单的按钮示例
1 | <Window.Resources> |
使用
1 | <Button Style="{StaticResource JianButton}" /> |
自定义Button
/Resources/StyleButton.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
/Resources/OverwrideDefaultControlStyles.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> |
自定义圆角Button
Button实现圆角很简单只需Button ContentTemplate修改样式模板就可以实现了。
但是这样无法达到复用,比如另一个按钮设计的不需要圆角样式又用的同一套怎么办?
当然可以在把样式复制下然后改一个样式名字修改下样式就可以了。
但是这样两份样式几乎一样,只不过一个圆角一个不是。
所以我们可以通过自定义Button控件给Button新增一个依赖属性完成这个功能。
添加属性
1 | using System.Windows; |
定义样式
/Resources/StyleZRoundButton.xaml
1 | <ResourceDictionary |
/Resources/OverwrideDefaultControlStyles.xaml
1 | <ResourceDictionary |
全局引用
Application中添加引用
1 | <Application |
使用
定义好这个属性就可以在控件中使用了
1 | <view:ZRoundButton |
其中主要是BorderRadius="5"
用来调整圆角。
我们再看看以前要实现就需要以下的代码,是不是感觉比以前简便多了。
1 | <Button |
自定义ListBoxItem
/Resources/StyleListBoxItem.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
/Resources/OverwrideDefaultControlStyles.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
使用
1 | <ListBox |
报错
System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=’System.Windows.Controls.ItemsControl’, AncestorLevel=’1’’. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is ‘ComboBoxItem’ (Name=’’); target property is ‘HorizontalContentAlignment’ (type ‘HorizontalAlignment’)
原因是ListBoxItem必须包含下面的两个属性
1 |
|
and use it:
1 | <ListView ItemContainerStyle="{StaticResource ZItemContainerStyle}"/> |
内部裁剪的Border
1 | using System; |
调用方式
引用
1 | xmlns:util="clr-namespace:ZJClassTool.Utils" |
使用
1 | <Views:ZClippingBorder |
自定义滚动条
/Resources/StyleScrolllview.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
全局样式设置
/Resources/OverwrideDefaultControlStyles.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
自定义ComboBox
/Resources/StyleComboBox.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
使用方式
1 | <ComboBox |
WPF全局样式设置
/Resources/OverwrideDefaultControlStyles.xaml
1 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:SchoolClient.Views"> |
Application中引用
1 | <Application |
启动页不唯一怎么办?
我们知道设置启动页有两种方式
- 添加窗口
生成操作
设置为ApplicationDefinition
- 添加cs代码文件,Main方法中运行窗口
但是用以下代码的方式创建窗口,设置的全局样式是不生效的,只有通过StartupUri="Wins/Welcome.xaml"
设置启动页才能使全局样式生效
1 | public partial class MyApp : Application |
但是通过StartupUri
只能设置唯一的启动页,怎么办呢?
我们可以添加一个欢迎页面作为中间页面,这个页面再决定跳转到那个页面,这样设置的全局样式就生效了。