WPF学习笔记(2)--样式(Style)
来源:互联网 发布:淘宝要求提供质检报告 编辑:程序博客网 时间:2024/05/19 03:28
在网上查了查WPF,看到了关于样式的文章,我试了一下,一起看看吧
在WPF中Style有3种使用方式:
一. Implicit Style(默认的样式)
样式的作用范围为整个XAML文件,如果你没有为你的控件指定样式的话,系统将会使用默认的样式.
二. Named Style
为控件指定特定的样式.
三. Derived Style
样式的继承.
<Window x:Class="WindowsApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Style" Height="300" Width="300"
>
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Control.Background" Value="red"/>
</Style>
<Style TargetType="{x:Type Button}" x:Key="BlueButton">
<Setter Property="Control.Background" Value="blue"/>
</Style>
<Style TargetType="{x:Type Button}" x:Key="YellowFont" BasedOn="{StaticResource BlueButton}">
<Setter Property="Control.Foreground" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Button Name="Button1" Margin="73,0,119,71" Height="25" VerticalAlignment="Bottom">Button1</Button>
<Button Name="Button2" Style="{StaticResource BlueButton}" Height="25" Margin="85,60,107,0" VerticalAlignment="Top">Button2</Button>
<Button Name="Button3" Style="{StaticResource YellowFont}" Margin="146,120,46,122" >Button3</Button>
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Style" Height="300" Width="300"
>
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Control.Background" Value="red"/>
</Style>
<Style TargetType="{x:Type Button}" x:Key="BlueButton">
<Setter Property="Control.Background" Value="blue"/>
</Style>
<Style TargetType="{x:Type Button}" x:Key="YellowFont" BasedOn="{StaticResource BlueButton}">
<Setter Property="Control.Foreground" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Button Name="Button1" Margin="73,0,119,71" Height="25" VerticalAlignment="Bottom">Button1</Button>
<Button Name="Button2" Style="{StaticResource BlueButton}" Height="25" Margin="85,60,107,0" VerticalAlignment="Top">Button2</Button>
<Button Name="Button3" Style="{StaticResource YellowFont}" Margin="146,120,46,122" >Button3</Button>
</Grid>
</Window>
第一个Style对应Implicit Style(默认的样式),第二个Style对应Named Style(特定样式),每三个Style对应Derived Style(样式的继承),而第三个也最有意思
<Style TargetType="{x:Type Button}" x:Key="YellowFont" BasedOn="{StaticResource BlueButton}">
<Setter Property="Control.Foreground" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
<Setter Property="Control.Foreground" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
他是说当鼠标在按钮上移动时,控件的前景色为红色,看在WPF中对外观的操作多方便,只要设置几个属性就行,要是在WinForm中,要写事件,才能实现控件这种效果,要是想多个控件都实现,那就必须封装一个控件了,看来还是WPF简单,学习,学习,学习。。。。
- WPF学习笔记(2)--样式(Style)
- WPF 中的样式(Style)
- WPF样式(Style)入门
- WPF学习笔记(1):样式(Style)与模板(Template)
- WPF学习笔记(1):样式(Style)与模板(Template)
- WPF入门(六)样式Style
- 学习笔记:windows phone 学习之 style(样式)
- React Native 学习笔记(五)-- 样式Style
- WPF学习笔记(2):在Style中设置ToolTip的问题.
- WPF学习笔记--样式
- wpf学习笔记-样式
- WPF样式(Style)与模板(Template)
- WPF样式(Style)与模板(Template)
- 16.WPF样式(Style)与模板(Template)
- WPF样式(Style)与模板(Template)
- WPF 之 样式 Style
- HTML && CSS 学习笔记(1)CSS(关键词:CSS/样式表/style)
- silverlight学习笔记。Style的继承(BaseOn)默认样式的问题
- 成为08奥运的志愿者
- 【06.11.27】【世界电影史上影片电影合集】【讯雷下载】
- WPF学习笔记(1)--菜单
- Spring2.0,Struts2.0,SpringSide 2.0--SpringSide 2.0M1 Release Notes
- 作为编辑器程序员,不能不用QT啊
- WPF学习笔记(2)--样式(Style)
- 关于c++的多态的一些重要问题和原则
- VxWorks操作系统复位实战(二)[by Progsoft]
- 使用sprintf及sscanf函数进行CString与CTime之间的转换
- 用atl写的缩略图组件
- c#常用函数方法及sql时间
- Tapestry4改进运行效率的实现方法
- [学习的脚步]初次使用string
- 用AXIS开发WEBSERVICE