原文地址:http://www.cnblogs.com/Joetao/articles/2074727.html

<UserControl x:Class="StyleDemo.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<!--
样式学习
(一)样式模板的基本知识
改变控件外观有2种方式
(1)Style
用Style的x:Key值来引用这个样式
用TargetType来声明作用的目标的类型(Button、ComboBox、Grid等Control)
通过Setter来声明作用目标的属性,每个Setter都要指定Property和Value
一个Style可以添加多个Seter
(二)样式的作用域
(1)全局样式
<Application.Resource>
</Application.Resource>
(2)局部样式
<UserControl.Resources>
</UserControl.Resources>
(3)内部样式
<Button Content="红色">
<Button.Style>
<Style TargetType="Button">
</Style>
</Button.Style>
</Button>
(三)控件模板
尽管通过属性可以改变控件的外观,但是这种改变是有限的。比喻要制作一个圆形按钮
控件模板也是样式的一个类型,对于一般样式我们的Property可以是Background、FontSize、Margin等简单属性
而对于控件模板样式的Property是Template,而Value是一个ControlTemplate

-->
<!--定义用户控件资源-->
<UserControl.Resources>
<!--Button样式1-->
<Style x:Key="buttonStyle1" TargetType="Button" >
<Setter Property="Background" Value="#ffee00"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Margin" Value="10,10,10,10"/>
</Style>
<!--Button样式2-->
<Style x:Key="buttonStyle2" TargetType="Button">
<Setter Property="Background" Value="White"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Margin" Value="25,25,25,25"/>
</Style>

<!--定义模板样式1-->
<Style x:Key="templateButtonStyle1" TargetType="Button">
<!--使用模板属性-->
<Setter Property="Template">
<Setter.Value>
<!--定义模板1-->
<ControlTemplate TargetType="Button">
<Grid Cursor="Hand" Margin="20">
<Ellipse Width="150" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="Gold" Offset="1"/>
<GradientStop Color="White" Offset="0"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<!--
使用TemplateBinding
TemplateBinding引用按钮本身的Content属性值,这样显示的内容不是“控件模板1”,而是后面的应用控件模板样式“我的控件”作为按钮内容
-->
<TextBlock Text="{TemplateBinding Content}" FontSize="24" Foreground="Blue" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--定义模板样式1-->
<Style x:Key="templateButtonStyle2" TargetType="Button">
<!--使用模板属性-->
<Setter Property="Template">
<Setter.Value>
<!--定义模板1-->
<ControlTemplate TargetType="Button">
<Grid Cursor="Hand" Margin="20">
<Ellipse Width="130" Height="130" HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="Red" Offset="1"/>
<GradientStop Color="White" Offset="0"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="控件模板2" FontSize="24" Foreground="Blue" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Orientation="Horizontal">
<!--应用样式1的按钮-->
<Button Content="蓝色" Width="150" Height="80" Style="{StaticResource buttonStyle1}"/>
<!--应用样式2的按钮-->
<Button Content="白色" Width="150" Height="80" Style="{StaticResource buttonStyle2}"/>
<Button Content="红色" Width="150" Height="80">
<!--内联样式-->
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Margin" Value="25,25,25,25"/>
</Style>
</Button.Style>
</Button>
<!--应用全局样式-->
<Button Width="200" Height="80" Content="全局样式" Style="{StaticResource buttonStyleGlobal}"/>
<!--应用模板样式1-->
<Button Style="{StaticResource templateButtonStyle1}" Content="我的控件"/>
<!--应用模板样式2-->
<Button Style="{StaticResource templateButtonStyle2}" Content="我的控件"/>

</StackPanel>
</Grid>
</UserControl>

(转) silverlight 样式学习的更多相关文章

  1. 11种常用css样式学习大结局滚动条与显示隐藏

    滚动条展示 overflow-x: hidden;/*是否对内容的左/右边缘进行裁剪*/overflow-y: hidden;/*是否对内容的上/下边缘进行裁剪*/overflow:scroll;/* ...

  2. 11种常用css样式之表格和定位样式学习

    table表格中border-collapse: collapse;/*表格边框是否合并*/border-spacing: 10px;/*表格边框之间的距离*/定位详情可以阅读position属性值4 ...

  3. 11种常用css样式之鼠标、列表和尺寸样式学习

    鼠标cursor常见样式crosshair;/*十字形状*/cursor:pointer;/*小手形状*/cursor:wait;/*等待形状*/cursor:text;/*默认 文本形状*/curs ...

  4. mono for android 自定义titleBar Actionbar 顶部导航栏 修改 样式 学习

    以前的我是没有做笔记的习惯的,学习了后觉得自己能记住,但是最近发现很多学的东西都忘记了,所有现在一有新的知识,就记下来吧. 最近又做一个mono for android 的项目 这次调整比较大,上次做 ...

  5. Silverlight 样式的灵活使用

    众所周知,Silverlight将界面设计与代码实现分开.即便如此,如果不能灵活地运用样式Style,开发的效率依然会比较低.比如,针对类似的TextBlock,你可能需要反复地在设计器xaml中复制 ...

  6. Silverlight Telerik 学习之主题的设置

    Telerik控件版本:RadControls for Silverlight Q1 2013 当前的Telerik Silverlight 控件支持以下主题 Office Black - 这是默认主 ...

  7. Silverlight 2学习笔记一:初识Silverlight

    Silverlight,问世至今已有好一段时日了,向来只是只闻其名,不知其实,今天终于对Silverlight有了点初步的了解. 一.Silverlight是什么?Sliverlight是基于.NET ...

  8. 【转】RadControls for Silverlight(学习2-RadDataPager)

    引用地址:http://www.cnblogs.com/forrestsun/archive/2011/05/15/2046894.html <Grid x:Name="LayoutR ...

  9. 【转】RadControls for Silverlight(学习1-GridView)

    引用:Telerik(官 网:http://www.telerik.com/)是保加利亚的一个软件公司,专注于微软.Net平台的表示层与内容管理控件.我们提供高度稳定性和丰富性能的组件产品,并可应用在 ...

随机推荐

  1. Java 项目优化实战

    https://blog.coding.net/blog/java-coding-performance 1 Visual VM 2 优化一 2.1 背景 2.2 原实现 2.3 剖析 2.4 方案 ...

  2. 如何去掉Eclipse里面自动追加的一些注释!!!内详

    比如我创建一个类,勾选了自动生成main函数.他就来一个// TODO Auto-generated method stub比如我输入"try"然后自动补完try catch bl ...

  3. django 1.8 TEMPLE_DIR和STATICFILES_DIRS配置

    django 1.6后settings.py文件中没有了TEMPLATE_DIRS模板目录和STATICFILES_DIRS静态访问目录,需要手动添加,最近也遇到这个问题,把解决办法说一下 1.环境 ...

  4. delphi真随机数发生器

    当然不是绝对真随机,是相对真随机数 下载

  5. 使用 hexdump dump 文件内容

    名词解释 [dump] dump 是指把文件的内容,每个字节用2位十六进制数来表示的方式. 缘由 最近看矢泽久雄的<How Program Works>,了解到 dump “exe文件”( ...

  6. eclipse配置javaee环境

    笔者开发javaee项目时惯用myeclipse,但由于个人笔记本性能较低,myeclipse对内存的消耗极大,所以考虑换成eclipse开发.本文介绍eclipse配置javaee开发环境的一些体会 ...

  7. 百度地图API 与 jquery 同时使用时报 TypeError $(...) is null错误 失效的原因及解决办法

    在引用百度地图API后,发现jquery 根据id 找不到 form.但是对于别的控件没有问题. 在排除了 html加载的问题后. 上网查找 发现以下解决办法: 原因应该是有冲突的插件. 解决办法将 ...

  8. 易全解token获取

    //易全解app             string strClientID = "2016061711434943493606";             string str ...

  9. springAOP实现基于注解的数据源动态切换

    需求 代码实现读写数据库分离 武器 spring3.0以上版本 实现思路 1.继承org.springframework.jdbc.datasource.lookup.AbstractRoutingD ...

  10. javascript中onload事件如何绑定及执行顺序

    onload事件,顾名思义就是为了js程序能够在网页加载成功过后进行执行.对于放在head里面的js来说,非常必要. 如何给网页绑定onload,主要有三种方式: window.onload=func ...