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

ControlTemplate和DataTemplate区别:

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

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

<Windows.Resources>
<Style x:Key="btnstyle" TargetType="Button">
<Setter Property="Width" Value="80"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Foreground" Value="Pink"/>
<Setter Property="FontSize" Value="20"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="pink"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="false"/>
<Condition Property="FontSize" Value="20"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="Gold"/>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
<Window.Resources>

  

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

  

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

  

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

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

<Window x:Class="WPFXAMLTest.WindowControlTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowControlTemplate" Height="" Width="">
<Grid Background="Yellow">
<Button Width="" Height="" Background="Cyan">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<Rectangle Width="" Height="" Fill="{TemplateBinding Background}" RadiusX="" RadiusY=""/>
<ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
</Grid>
</ControlTemplate>
</Button.Template>
<Button.Content>
<Grid>
<Ellipse Fill="Red" Width="" Height=""/>
<TextBlock Text="DebugLZQ" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Button.Content>
</Button>
<Button HorizontalAlignment="Left" Margin="105,190,0,0" VerticalAlignment="Top" Width="">
<Button.Template>
<ControlTemplate >
<TextBlock Text="DebugLZQ" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Window>

<Style  x:Key="btnstyle" TargetType="Button">
<Setter Property="Width" Value="80"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Foreground" Value="Pink"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Template"><!--所有Control控件都有Style和Template属性,前者用来控制控件的原有属性;后者用来定义控件的内部结构,对控件外观和形状进行改变 -->
<Setter.Value>
<ControlTemplate TargetType="Button"><!--ControlTemplate 描述控件的行为和样式-->
<Grid Width="80" Height="50">
<Image Source="Images/1.png" Stretch="Fill" />
<!---->
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect ShadowDepth="4"/>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter> </Style>

  

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

  

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

  

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

  

<Style x:Key="lstboxstyle" TargetType="ListBox">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<Image Source="{Binding ImgPath}" Width="70" Height="70" Margin="0"/>
<TextBlock Text="{Binding ImgTxt}" HorizontalAlignment="Center" Margin="5"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

  

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

  

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

  

<Style x:Key="lstboxstyle2" TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate><!-- ItemsPanelTemplate指定控件子项的布局样式,Combox,TreeView,DataGrid,TabelControl也都均有此属性-->
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate"><!-- ItemTemplate定义子项的外观-->
<Setter.Value>
<DataTemplate><!-- 返回值DataTemplate-->
<StackPanel>
<Image Source="{Binding ImgPath}" Width="70" Height="70" Margin="0"/>
<TextBlock Text="{Binding ImgTxt}" HorizontalAlignment="Center" Margin="5" Foreground="Pink"/><!--可以这里改-->
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle"><!--也能在这里改,也能直接在TextBlock里改-->
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="FontSize" Value="20"/>
</Style>
</Setter.Value>
</Setter>
</Style>

  

<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. DUBBO配置规则详解

    研究DUBBO也已经大半年了,对它的大部分源码进行了分析,以及对它的内部机制有了比较深入的了解,以及各个模块的实现.DUBBO包含很多内容,如果想了解DUBBO第一步就是启动它,从而可以很好的使用它, ...

  2. Scala中()与{}

    Scala中()与{}的关系 在Scala中有些时候()和{}是可以相互替换的,但是如果不弄清楚到底什么时候该用(),什么时候该用{},什么时候二者可以相互替换,那么在写代码的时候难免为出错,并且自己 ...

  3. jersey学习笔记

    最近一个项目用到了jersey,我只是负责前端.也借此机会好好了解一下REST webservice及一大推名词. http://redhacker.iteye.com/blog/1914105 1. ...

  4. 转:getContextPath、getServletPath、getRequestURI的区别

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

  5. java web 中的WEB-INF文件夹

    WEB-INF下的东西是禁止直接访问的.如果这个页面是你的,要想让人访问最好不要放在这个目录下.如果一定放在那里.你可以使用:request.getRequestDispatcher("/W ...

  6. ASP.NET MVC 使 Controller 的 Action 只接受 Ajax 请求。

    首先,ajax 请求跟一般的 web 请求本质是相同的,都是 http 请求.理论上服务器端是无法区分该次请求是不是 ajax 请求的,但是,既然标题都已经说了,那么肯定是有办法做的. 在 ajax ...

  7. Spark Structured Stream 2

    ❤Limitations of DStream API Batch Time Constraint application级别的设置. 不支持EventTime event time 比process ...

  8. The transaction associated with this command is not the connection's active transaction

    The fix is fairly simple: if you want a Dapper query to participate in a connection, explicitly deno ...

  9. 微信公众平台如何与Web App结合?

    Web App简而言之就是为移动平台而优化的网页,它可以表现得和原生应用一样,并且克服了原生应用一些固有的缺点.一般而言Web App最大的入口是浏览器,但现在微信公众平台作为新兴的平台,结合其内置浏 ...

  10. TransactionScope事务使用

    using (System.Transactions.TransactionScope T_Scope = new System.Transactions.TransactionScope()) { ...