WPF中的Style类似于Web应用程序中的CSS,它是控件的一个属性,属于资源的一种。

ControlTemplate和DataTemplate区别:

ControlTemplate用于改变控件原来的形状(一般定义在Style中,给控件穿上一层新的外壳,改变这个控件的外观),而DataTemplate不改变控件原来的形状(给某个控件加上数据,相当于给控件显示它想显示的内容(可能会有多种控件组合))。

通常把Style定义在Resources中,使用方式如下:

  1. <Windows.Resources>
  2. <Style x:Key="btnstyle" TargetType="Button">
  3. <Setter Property="Width" Value="80"/>
  4. <Setter Property="Height" Value="50"/>
  5. <Setter Property="Foreground" Value="Pink"/>
  6. <Setter Property="FontSize" Value="20"/>
  7. <Style.Triggers>
  8. <Trigger Property="IsMouseOver" Value="true">
  9. <Setter Property="Background" Value="pink"/>
  10. </Trigger>
  11. <MultiTrigger>
  12. <MultiTrigger.Conditions>
  13. <Condition Property="IsMouseOver" Value="false"/>
  14. <Condition Property="FontSize" Value="20"/>
  15. </MultiTrigger.Conditions>
  16. <MultiTrigger.Setters>
  17. <Setter Property="Background" Value="Gold"/>
  18. </MultiTrigger.Setters>
  19. </MultiTrigger>
  20. </Style.Triggers>
  21. </Style>
  22. <Window.Resources>

  

  1. <Button x:Name="button1" Style="{StaticResource btnstyle}" Content="button1" >

  

  1. button1.style=(style)Resources["btnstyle"];

  

如果只需对控件进行小幅度修饰(调整大小、位置、字体、颜色等)就用style;如果需要改变控件的外观和行为就用controlTemplate(形状、事件触发如鼠标停留效果等)。在实际项目中,经常把Template定义在Style中,通过Style 中的Property来设置控件的Template属性。

WPF中的所有COntrol控件都有Template属性。下面以代码的形式,展现WPF中常用的Template。

  1. <Window x:Class="WPFXAMLTest.WindowControlTemplate"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="WindowControlTemplate" Height="" Width="">
  5. <Grid Background="Yellow">
  6. <Button Width="" Height="" Background="Cyan">
  7. <Button.Template>
  8. <ControlTemplate TargetType="Button">
  9. <Grid>
  10. <Rectangle Width="" Height="" Fill="{TemplateBinding Background}" RadiusX="" RadiusY=""/>
  11. <ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
  12. </Grid>
  13. </ControlTemplate>
  14. </Button.Template>
  15. <Button.Content>
  16. <Grid>
  17. <Ellipse Fill="Red" Width="" Height=""/>
  18. <TextBlock Text="DebugLZQ" VerticalAlignment="Center" HorizontalAlignment="Center"/>
  19. </Grid>
  20. </Button.Content>
  21. </Button>
  22. <Button HorizontalAlignment="Left" Margin="105,190,0,0" VerticalAlignment="Top" Width="">
  23. <Button.Template>
  24. <ControlTemplate >
  25. <TextBlock Text="DebugLZQ" VerticalAlignment="Center" HorizontalAlignment="Center"/>
  26. </ControlTemplate>
  27. </Button.Template>
  28. </Button>
  29. </Grid>
  30. </Window>

  1. <Style x:Key="btnstyle" TargetType="Button">
  2. <Setter Property="Width" Value="80"/>
  3. <Setter Property="Height" Value="50"/>
  4. <Setter Property="Foreground" Value="Pink"/>
  5. <Setter Property="FontSize" Value="20"/>
  6. <Setter Property="Template"><!--所有Control控件都有Style和Template属性,前者用来控制控件的原有属性;后者用来定义控件的内部结构,对控件外观和形状进行改变 -->
  7. <Setter.Value>
  8. <ControlTemplate TargetType="Button"><!--ControlTemplate 描述控件的行为和样式-->
  9. <Grid Width="80" Height="50">
  10. <Image Source="Images/1.png" Stretch="Fill" />
  11. <!---->
  12. <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
  13. </Grid>
  14. <ControlTemplate.Triggers>
  15. <Trigger Property="IsMouseOver" Value="true">
  16. <Setter Property="Effect">
  17. <Setter.Value>
  18. <DropShadowEffect ShadowDepth="4"/>
  19. </Setter.Value>
  20. </Setter>
  21. </Trigger>
  22. </ControlTemplate.Triggers>
  23. </ControlTemplate>
  24. </Setter.Value>
  25. </Setter>
  26.  
  27. </Style>

  

  1. <Button x:Name="button1" Style="{StaticResource btnstyle}" Content="button1" Click="Button_Click" Margin="30,23,393,238"/>

  

  1. <Style x:Key="btnstyle2" TargetType="Button">
  2. <Setter Property="Width" Value="80"/>
  3. <Setter Property="Height" Value="50"/>
  4. <Setter Property="ContentTemplate"><!--2.ContentTemplate不改变控件行为的基础上,只对控件内容进行更改 -->
  5. <Setter.Value>
  6. <DataTemplate><!--返回值是 DataTemplate-->
  7. <Grid>
  8. <Image Source="Images/1.png" Stretch="Fill" />
  9. <TextBlock Text="{TemplateBinding Content}" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Pink" />
  10. </Grid>
  11. </DataTemplate>
  12. </Setter.Value>
  13. </Setter>
  14. </Style>

  

  1. <Button x:Name="button2" Style="{StaticResource btnstyle2}" Content="button2" Margin="30,117,392,144" />

  

  1. <Style x:Key="lstboxstyle" TargetType="ListBox">
  2. <Setter Property="ItemTemplate">
  3. <Setter.Value>
  4. <DataTemplate>
  5. <StackPanel>
  6. <Image Source="{Binding ImgPath}" Width="70" Height="70" Margin="0"/>
  7. <TextBlock Text="{Binding ImgTxt}" HorizontalAlignment="Center" Margin="5"/>
  8. </StackPanel>
  9. </DataTemplate>
  10. </Setter.Value>
  11. </Setter>
  12. </Style>

  

  1. <ListBox Style="{StaticResource lstboxstyle }" Height="214" HorizontalAlignment="Left" Margin="226,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="153" />

  

  1. //Binding ListBox
  2. ArrayList list = new ArrayList();
  3. list.Add(new { ImgPath="Images/1.png",ImgTxt="DebugLZQ1"});
  4. list.Add(new { ImgPath = "Images/1.png", ImgTxt = "DebugLZQ2" });
  5. list.Add(new { ImgPath = "Images/1.png", ImgTxt = "DebugLZQ3" });
  6.  
  7. listBox1.ItemsSource = listBox2.ItemsSource = list;

  

  1. <Style x:Key="lstboxstyle2" TargetType="ListBox">
  2. <Setter Property="ItemsPanel">
  3. <Setter.Value>
  4. <ItemsPanelTemplate><!-- ItemsPanelTemplate指定控件子项的布局样式,Combox,TreeView,DataGrid,TabelControl也都均有此属性-->
  5. <StackPanel Orientation="Horizontal" />
  6. </ItemsPanelTemplate>
  7. </Setter.Value>
  8. </Setter>
  9. <Setter Property="ItemTemplate"><!-- ItemTemplate定义子项的外观-->
  10. <Setter.Value>
  11. <DataTemplate><!-- 返回值DataTemplate-->
  12. <StackPanel>
  13. <Image Source="{Binding ImgPath}" Width="70" Height="70" Margin="0"/>
  14. <TextBlock Text="{Binding ImgTxt}" HorizontalAlignment="Center" Margin="5" Foreground="Pink"/><!--可以这里改-->
  15. </StackPanel>
  16. </DataTemplate>
  17. </Setter.Value>
  18. </Setter>
  19. <Setter Property="ItemContainerStyle"><!--也能在这里改,也能直接在TextBlock里改-->
  20. <Setter.Value>
  21. <Style TargetType="ListBoxItem">
  22. <Setter Property="FontSize" Value="20"/>
  23. </Style>
  24. </Setter.Value>
  25. </Setter>
  26. </Style>

  

  1. <ListBox Style="{StaticResource lstboxstyle2 }" Height="131" HorizontalAlignment="Left" Margin="42,256,0,0" Name="listBox2" VerticalAlignment="Top" Width="417" />

  

WPF Style和Template的更多相关文章

  1. WPF DataGrid Custommization using Style and Template

    WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...

  2. Bootstrap WPF Style(二)--Glyphicons 字体图标

    介绍 关于Glyphicons字体图标,首先给出友情链接 Glyphicons 这个项目是在Bootstrap WPF Style项目基础上做的,详见http://www.cnblogs.com/ts ...

  3. WPF QuickStart系列之样式和模板(Style and Template)

    在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到WPF中的样式和模板技术.简单来说,如果我们需要简单的给一个Button设置宽,高,Marg ...

  4. WPF Style设置和模板化Template

    WPF样式设置和模板化是一套功能(样式,模板,触发器和演示图版),可以为产品设置统一外观.类似于html的css,可以快速的设置一系列属性值到控件. 案例:ButtonStyle 这里创建了一个目标类 ...

  5. wpf中在style的template寻找ControlTemplate和DataTemplate的控件

    一.WPF中的两棵树 WPF中每个控件的Template都是由ControlTemplate构成,ControlTemplate包含了构成该控件的各种子控件,这些子控件就构成了VisualTree:而 ...

  6. Bootstrap WPF Style,Bootstrap风格的WPF样式

    简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...

  7. wpf 的各个template

    --转载 在使用TabControl.ListView.Menu.TreeView的时候被各种Template搞得头昏眼花,决心把这个问题搞清楚,究竟什么时候该用什么Template?这是个麻烦的问题 ...

  8. C#工具:Bootstrap WPF Style,Bootstrap风格的WPF样式

    简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...

  9. WPF Style

      <Application x:Class="WzlyTool.App" xmlns="http://schemas.microsoft.com/winfx/20 ...

随机推荐

  1. express4.X 笔记

    express是node的web框架,更新频繁,3.X到4.X有了很大的改变.网上的例子,各种版本的都有,为了以后方便,现在重新认真看一遍4.X的API,统一以后的使用方法.在J2EE上落后了,在ex ...

  2. 理解Struts2的Action中的setter方法是怎么工作的

    接触过webwork和Struts2的同行都应该知道, 提交表单的时候,只要Action中的属性有setter 方法,这些表单数据就可以正确赋值到Action中属性里:另外对于Spring配置文件中声 ...

  3. Nodejs异步框架——async

    上次的网页爬虫写完后,又打算做一个爬图的工具.前两天已经写好了代码.思路如下: 分析页面还是采用cheerio,对<div>中的img进行分析抽取,拿到图片的url.然后用childpro ...

  4. Linq to Object 的简单使用示例

    语言集成查询 (LINQ) 是 Visual Studio 2008 中引入的一组功能,可为 C# 和 Visual Basic 语言语法提供强大的查询功能. LINQ 引入了标准易学的数据查询和更新 ...

  5. Mac iTerm2登陆CentOS提示warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

    [报错原因]:没有utf-8这个语系(没添加语言_国名前缀),LC_ALL又没设定值. 服务端解决方法: 在远程系统上, /etc/environment 加入以下两行,重新登陆即可. LANG=en ...

  6. 截图-----Selenium快速入门(十二)

    在自动化测试过程中,截图是常见的操作,因为有时候单靠程序无法判断是否已得到期望的结果,所以需要截图判断.又或者截图是作为判断的存证.Selenium的截图操作也是非常简单,而且自带了一个文件操作类Fi ...

  7. Android 为库(library)创建不同编译环境

    项目中需要导入库,一般有两种情况,一种是直接路径导入,一种是导入库的 aar 文件. 1. 设置库项目 1. 在库项目的 src 目录下设置 debug 目录,里面可以添加代码或者 res 文件夹. ...

  8. SqlAlchemy操作(一)

    博客转载于 http://www.cnblogs.com/haiyan123/p/8270520.html 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB ...

  9. day 82 Django Admin组件.

    一.先建表环境 modules文件 from django.db import models # Create your models here. from django.contrib.auth.m ...

  10. FFmpeg软硬解和多线程解码

    一. AVCodecContext解码上下文 1.avcodec_register_all() : 注册所有的解码器 2.AVCodec *avcodec_find_decoder(enum AVCo ...