当前位置:网站首页>WPF 学习笔记《WPF样式基础》
WPF 学习笔记《WPF样式基础》
2022-08-03 07:59:00 【太阳风暴】
一、样式简介
参考资料:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.style?view=windowsdesktop-6.0
为了方便的设计WPF界面的样式,可以直接在界面xaml中指定对应的样式代码,用作设计样式即可。当然xaml文件只是描述如何去生成c#后台代码而已,同样的,style本质上也是一个类,也不仅仅是为了生成后台代码。style的样式我们也可以在后台去设置。我学的基础只限于前端样式xaml文件的书写
1、样式位置
样式属于代码片段,可以放置于每一个窗口也是(window***.xaml)文件的 Resources 内部,当然也可把该样式代码放置于app.xaml内部的资源中,那么放置在app.xaml的样式文件会在全局生效,有点儿类似 CSS代码样式表的层叠效果。
<!--全局设置:作用于全局-->
<Application x:Class="EventLearn.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:EventLearn" StartupUri="UseStyle.xaml">
<Application.Resources>
<Style TargetType="{x:Type Button}"> <!--样式代码--> </Style>
</Application.Resources>
</Application>
<!--局部样式:只作用于当前窗口-->
<Window x:Class="EventLearn.UseStyle" 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:EventLearn" mc:Ignorable="d" Title="UseStyle" Height="450" Width="800">
<Window.Resources>
<Style TargetType="Button"> <!--样式代码--> </Style>
</Window.Resources>
<Grid>
</Grid>
</Window>
2、样式写法
资源以 <style> 标签标注起来的样式,在 <style> 可以指定使用 TargetType 描述样式设置于 什么样的控件,还可以跟上 x:Key 来指定样式的标志,在控件指定使用哪一种样式即可
在内部可以使用<setter>标签包裹来表示如下,<setter>内部使用的是 Property - Value 来表示一种特性
设置一个按钮的样式,内部可以使用xaml的展开式来描述值
<Window x:Class="EventLearn.UseStyle" 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:EventLearn" mc:Ignorable="d" Title="UseStyle" Height="450" Width="800">
<Window.Resources>
<Style TargetType="Button"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="Background" Value="Black"/> </Style>
<Style x:Key="NewBtnStyle" TargetType="Button"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="BlueViolet"/> </Setter.Value> </Setter> </Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="按钮一" Margin="20" Grid.Column="0" Grid.Row="0"/>
<Button Content="按钮二" Margin="20" Grid.Column="0" Grid.Row="1"/>
<Button Content="按钮三" Margin="20" Grid.Column="0" Grid.Row="2"/>
<Button Content="按钮四" Margin="20" Grid.Column="0" Grid.Row="3"/>
<Button Content="按钮一" Style="{
StaticResource NewBtnStyle}" Margin="20" Grid.Column="1" Grid.Row="0" />
<Button Content="按钮二" Margin="20" Style="{
StaticResource NewBtnStyle}" Grid.Column="1" Grid.Row="1"/>
<Button Content="按钮三" Margin="20" Grid.Column="1" Grid.Row="2"/>
<Button Content="按钮四" Margin="20" Grid.Column="1" Grid.Row="3"/>
</Grid>
</Window>
- 效果图:
二、静态样式
静态样式,一般会设置在具有动态效果的控件(如Button)以便打基础,动态交互就需要使用到动态样式的设置、以及不具有动态交互的控件(如 Border、Label、TextBlock等)
如下代码
<Window x:Class="EventLearn.UseStyle" 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:EventLearn" mc:Ignorable="d" Title="UseStyle" Height="450" Width="800">
<Window.Resources>
<Style TargetType="Button"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="Background" Value="Black"/> </Style>
<Style x:Key="NewBtnStyle" TargetType="Button"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="BlueViolet"/> </Setter.Value> </Setter> </Style>
<Style TargetType="Border" x:Key="Logo"> <Setter Property="CornerRadius" Value="10 2 10 2"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="CadetBlue"/> <Setter Property="Margin" Value="20"/> <Setter Property="Background" Value="Chocolate"/> </Style>
<Style TargetType="TextBlock"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="FontSize" Value="16"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="按钮一" Margin="20" Grid.Column="0" Grid.Row="0"/>
<Button Content="按钮二" Margin="20" Grid.Column="1" Grid.Row="0"/>
<Border Style="{
StaticResource Logo}" Grid.Column="2" Grid.Row="0">
<TextBlock Text="WPF"/>
</Border>
</Grid>
</Window>
三、动态样式
大部分控件是具有交互功能的,如果需要创建动态效果,如鼠标移动到控件上、鼠标按下、释放等
<Window x:Class="EventLearn.UseStyle" 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:EventLearn" mc:Ignorable="d" Title="UseStyle" Height="450" Width="800">
<Window.Resources>
<Style TargetType="Button"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="Background" Value="Black"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="BorderBrush" Value="Aquamarine"/> <Setter Property="BorderThickness" Value="4"/> </Trigger> </Style.Triggers> </Style>
<Style x:Key="NewBtnStyle" TargetType="Button"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="BlueViolet"/> </Setter.Value> </Setter> </Style>
<Style TargetType="Border" x:Key="Logo"> <Setter Property="CornerRadius" Value="10 2 10 2"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="CadetBlue"/> <Setter Property="Margin" Value="20"/> <Setter Property="Background" Value="Chocolate"/> </Style>
<Style TargetType="TextBlock"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="FontSize" Value="16"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="按钮一" Margin="20" Grid.Column="0" Grid.Row="0"/>
<Button Content="按钮二" Margin="20" Grid.Column="1" Grid.Row="0"/>
<Border Style="{
StaticResource Logo}" Grid.Column="2" Grid.Row="0">
<TextBlock Text="WPF"/>
</Border>
</Grid>
</Window>
- 示例:
四、样式继承
可以使用 BaseOn 基础默认基础样式,当然已有的样式那个继承得样式是按照优先级,越具体的优先级越高。没设置的就默认继承之前最近一次父对象。没有BaseOn的话就按照的是控件的控件模板渲染
<Window x:Class="EventLearn.UseStyle" 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:EventLearn" mc:Ignorable="d" Title="UseStyle" Height="450" Width="800">
<Window.Resources>
<Style x:Key="Defate" TargetType="Button"> <Setter Property="Foreground" Value="AntiqueWhite"/> <Setter Property="Background" Value="Black"/> </Style>
<Style BasedOn="{StaticResource Defate}" x:Key="NewBtnStyle" TargetType="Button"> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="BlueViolet"/> </Setter.Value> </Setter> </Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="按钮一" Style="{
StaticResource Defate}" Margin="20" Grid.Column="0" Grid.Row="0"/>
<Button Content="按钮二" Style="{
StaticResource NewBtnStyle}" Margin="20" Grid.Column="1" Grid.Row="0"/>
</Grid>
</Window>
五、补充
待定
边栏推荐
- ArcEngine(六)用tool工具实现拉框放大缩小和平移
- 积分商城系统设计
- 解决移动端有纵向滚动条但是不能滚动的问题
- 力扣(LeetCode)214. 打家劫舍 II(2022.08.02)
- LeetCode 264:丑数
- [Kaggle combat] Prediction of the number of survivors of the Titanic (from zero to submission to Kaggle to model saving and restoration)
- 内存模型之可见性
- redis AOF持久化个人理解
- 001-进程与线程
- 如何在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster?
猜你喜欢
mysql服务器上的mysql这个实例中表的介绍
审批流设计
使用pipreqs导出项目所需的requirements.txt(而非整个环境)
FusionAccess软件架构、FusionAccess必须配置的四个组件、桌面发放流程、虚拟机组类型、桌面组类型
热部署系统实现
22-08-02 西安 尚医通(02)Vscode、ES6、nodejs、npm、Bable转码器
WordPress主题-B2美化通用子主题商业运营版
001-进程与线程
@Async注解的坑,小心
mysql5.7服务器The innodb_system data file 'ibdata1' must be writable导致无法启动服务器
随机推荐
frp:开源内网穿透工具
Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
二进制日志过期时间设置expire_logs_days
vs 2022无法安装 vc_runtimeMinmum_x86错误
uni-app 顶部选项卡吸附效果 demo(整理)
依赖注入(DI),自动配置,集合注入
mysql服务器上的mysql这个实例中表的介绍
实时目标检测新高地之#YOLOv7#更快更强的目标检测器
线性表
mysql的innodb存储引擎和myisam存储引擎的区别
0day_Topsec上网行为管理RCE
进程的创建
ArcEngine(一)加载矢量数据
ArcEngine (3) zoom in and zoom out through the MapControl control to achieve full-image roaming
Daily practice of PMP | Do not get lost in the exam-8.2 (including agility + multiple choice)
001-进程与线程
智能客服,还有多少AI泡沫?
工控机防勒索病毒浅析
推荐系统-排序层-精排模型:LR、GBDT、Wide&Deep、DCN、DIN、DIEN、MMOE、PLE
内存模型之可见性