原文地址: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. php常用的字符串函数

    addslashes -- 使用反斜线引用字符串 chr -- 返回相对应于 ascii 码值所指定的单个字符. chunk_split -- 将字符串分割成小块 count_chars --  返回 ...

  2. C# Using 用法

    using 语句允许程序员指定使用资源的对象应当何时释放资源.为 using 语句提供的对象必须实现 IDisposable 接口.此接口提供了 Dispose 方法,该方法将释放此对象的资源. 一起 ...

  3. submit 读取mb52数据

    方法一: data:list_tab type table of abaplist.   data:vlist(300) type c occurs 0 with header line. submi ...

  4. GO_order

    Gpos Q8RP81 有GO:0000015 P47437 有GO:0000049 P06535 有GO:0000155 Q99027 有GO:0000160 P35594 有GO:0000166 ...

  5. python学习笔记-多进程

    multiprocessing from multiprocessing import Process import time def f(name): time.sleep(2) print('he ...

  6. (三)Bootstrap.jar

    catalina.bat 在最后启动了bootstrap.jar, 传递了start作为参数(如果多个参数的话,start在尾部). 然后org.apache.catalina.startup.Boo ...

  7. MVC4.0 扩展辅助方法

    新年第一天上班,写个博客开头吧! 在MVC中,辅助类是很常见的,比如说,Html.TextBox().Html.DropDownListFor()等,这些都是微软帮我们封装好的,可以直接调用的,它们解 ...

  8. [[UIScreen mainScreen] bounds] 返回的屏幕尺寸不对

    在使用cocos2d-iphone 2.0生成项目的时候,用5s测试时全屏中上下一直有黑条,发现[[UIScreen mainScreen] bounds]返回的屏幕尺寸不是320*568的,而是32 ...

  9. 纯CSS实现Tooltip

    DEMO: span{ position:relative; display:inline-block; height:3em; width:3em; margin:0 0.4em; line-hei ...

  10. linux 查看php-fpm 进程数

    netstat -napo |grep "php-fpm" | wc -l