原文地址: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. c#.net全站防止SQL注入类的代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  2. selenium第一课(selenium+java+testNG+maven)

    selenium介绍和环境搭建 一.简单介绍 1.selenium:Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包 ...

  3. NGUI 使用EventDelegate.Add与UIInput.onSubmit、UIInput.onChange限定编辑框中的内容

    Unity中,使用NGUI,通常为某个控件(如按钮)绑定事件(单击.双击.拖拽.滚轮.选择等)都是用UIEventListener,比如: public void Bind() { UIEventLi ...

  4. Java重点之小白解析--浅谈HashMap与HashTable

    这是一个面试经常遇到的知识点,无论什么公司这个知识点几乎是考小白必备,为什么呢?因为这玩意儿太特么常见了,常见到你写一百行代码,都能用到好几次,不问这个问哪个.so!本小白网罗天下HashMap与Ha ...

  5. JavaScript 详说事件机制之冒泡、捕获、传播、委托

    DOM事件流(event  flow )存在三个阶段:事件捕获阶段.处于目标阶段.事件冒泡阶段. 事件捕获(event  capturing):通俗的理解就是,当鼠标点击或者触发dom事件时,浏览器会 ...

  6. easyui 使用问题积累

    一.<a>标签中onclick和javascript事件与grid的select事件执行的先后顺序 html: <a href='javascript:func();' onclic ...

  7. python 正则re模块

    re.match re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词. import re text = "JGood is a handsome boy, he ...

  8. Spring bean的作用域和生命周期

    bean的作用域 1.singleton,prototype, web环境下:request,session,gloab session 2.通过scope="" 来进行配置 3. ...

  9. IIS 的一些配置记录

    1.日志分析: URL:http://www.cnblogs.com/fish-li/p/3139366.html2.性能监视: 执行 perfmon.msc ,右键添加counter,添加web s ...

  10. ue4 重新生成ide project文件的命令行

    有时候换了机器,工程文件没了,通常是在编辑器里有个菜单项可以生成 但是有时编辑器自身都编不过,没法运行 这时需要调试代码,可以用命令行生成相应的工程文件: ../UnrealEngine/Genera ...