Windows Phone 四、控件模版

控件模版的概念

Windows Phone中每一个控件都有一个默认的模版,用于描述控件的内部组成结构和外观样式

相对于原本的样式外观操作,自定义模版的可自定义性更强

最基本的重写控件模版

 1     <Grid>
 2         <!--Button按钮居中对齐-->
 3         <Button HorizontalAlignment="Center">
 4             <!--原生控件模版设置的就是Template属性-->
 5             <Button.Template>
 6                 <!--控件模版类型-->
 7                 <ControlTemplate>
 8                     <!--Border本身支持圆角-->
 9                     <Border
10                         BorderBrush="White"
11                         BorderThickness="3"
12                         CornerRadius="10,20,10,10">
13                         <TextBlock
14                             Text="Button"
15                             TextAlignment="Center"
16                             VerticalAlignment="Center"/>
17                     </Border>
18                 </ControlTemplate>
19             </Button.Template>
20         </Button>
21     </Grid>

BorderBrush 颜色  BorderThickness 边框宽度  CornerRadius 四个圆角的弧度

Text 内容  TextAlignment 水平对齐方式  VerticalAlignment 垂直对齐方式

绑定属性和重用(自定义控件模版)

 1     <Page.Resources>
 2         <!--圆角Button模版-->
 3         <ControlTemplate x:Key="CornerButton" TargetType="Button">
 4             <!--Border本身支持圆角-->
 5             <Border
 6                         Background="{TemplateBinding Button.Background}"
 7                         BorderBrush="{TemplateBinding Button.BorderBrush}"
 8                         BorderThickness="{TemplateBinding Button.BorderThickness}"
 9                         CornerRadius="10,20,10,10">
10                 <!--TemplateBinding是用来在模版中绑定当前控件属性-->
11                 <TextBlock
12                             Text="{TemplateBinding Button.Content}"
13                             TextAlignment="Center"
14                             VerticalAlignment="Center"/>
15             </Border>
16         </ControlTemplate>
17         <!--统一所有按钮-->
18         <Style TargetType="Button">
19             <Setter Property="Template" Value="{StaticResource CornerButton}"/>
20         </Style>
21     </Page.Resources>
22     <Grid>
23         <!--圆角Button模版,资源引用-->
24         <Button
25             Content="Button"
26             Background="Aqua"
27             BorderBrush="HotPink"
28             BorderThickness="15"
29             HorizontalAlignment="Center"
30             Template="{StaticResource CornerButton}">
31         </Button>
32     </Grid>

图标按钮展示内容

Button派生自ContentControl,所有ContentControl都是由ContentPersenter展示Content属性

 1     <Grid>
 2         <Button>
 3             <Button.Content>
 4                 <SymbolIcon Symbol="Accept"/>
 5             </Button.Content>
 6             <Button.Template>
 7                 <ControlTemplate>
 8                     <Border
 9                         BorderBrush="White"
10                         BorderThickness="3" CornerRadius="10">
11                         <ContentPresenter/>
12                     </Border>
13                 </ControlTemplate>
14             </Button.Template>
15         </Button>
16     </Grid>
时间: 2024-08-28 11:59:55

Windows Phone 四、控件模版的相关文章

背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(WebView) 加载指定 HttpMethod 的请求 自定义请求的 http header app 与 js 的交互 示例1.演示 WebView 如何加载指定 HttpMethod 的请求以及如何自定义请求的 http headerWebApi/Controllers/WebViewPostController.cs /* * 用于 WebView 演示“如何加载指定 HttpMethod 的请求,以及如何自

实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)

实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624.aspx  译 Q:77811970 实现虚拟模式的原因之一 DataGridView控制只检索数据,因为它是必要的. 这就是所谓的 即时数据加载 . 如果你正在与一个非常大的表在一个远程数据库,例如,您可能希望避免启动延迟,只检索所需的数据显示和检索额外的数据只有当用户新行滚动到视图. 如果客户

Essential Diagram for Windows Forms绘图控件图解及下载地址

Essential Diagram for Windows Forms是一款可扩展的.高性能的.NET平台下的绘图控件,可用于开发像Microsoft Visio一样的交互式地绘图和图解应用程序,在节点存储图形对象,支持矢量和光栅图形. 具体功能: 支持多种导出格式:如位图.增强的元文件.SVG文件格式 控件采用清晰的MVC设计,把数据层.表现层和用户层分离 子节点属性可以从父节点继承,开发人员可以应用属性值到一个节点或所有子节点 支持在运行时添加自定义属性 多种线条节点和连接器,支持多种连接线

WPF编程宝典之控件模版(七)

将控件模版定义为资源,并使用StaticResource引用该资源 1 <Button Margin="10" Padding="5" Template="{StaticResource ButtonTemplaate}"> 2 A Simple Button with a Custom Template</Button> 控件模版的基本框架如下: 1 <Window.Resource> 2 <Contr

背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingStackPanel, WrapGrid

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类 - ItemsControl 的布局控件) OrientedVirtualizingPanel VirtualizingStackPanel WrapGrid 示例1.OrientedVirtualizingPanel(基类) 的示例Controls/CollectionControl/ItemsControlDemo/LayoutControl/OrientedVirtualizingPanelDemo.

背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar

原文:背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) AppBar CommandBar 示例1.AppBar 的示例Controls/NavigationControl/AppBarDemo.xaml <Page x:Class="Windows10.Controls.NavigationControl.AppBarDemo" xml

背水一战 Windows 10 (41) - 控件(导航类): Frame

原文:背水一战 Windows 10 (41) - 控件(导航类): Frame [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 示例Controls/NavigationControl/FrameDemo.xaml <Page x:Class="Windows10.Controls.NavigationControl.FrameDemo" xmlns="http://schemas.microsoft.com/w

背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

原文:背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹出类) ToolTip Popup PopupMenu 示例1.ToolTip 的示例Controls/FlyoutControl/ToolTipDemo.xaml <Page x:Class="Windows10.Controls.FlyoutControl.ToolTipDemo&q

背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox

原文:背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) TextBox PasswordBox 示例1.TextBox 的示例 1Controls/TextControl/TextBoxDemo1.xaml <Page x:Class="Windows10.Controls.TextControl.TextBoxDemo1" xmln