WPF:自定义控件之Button-自定义样式,共用样式
来源:互联网 发布:淘宝客服外包服务 编辑:程序博客网 时间:2024/06/11 23:53
WPF 中 最好的特性就是我们自己可以自定义控件样式,小到一个Button按钮,大到DataGrid或者TreeView控件。不过要想知道如何定义样式,需要我们了解控件的特性,结构。也就是控件由什么构成的。 WPF中的控件基本分为 内容控件与条目控件。内容控件可以理解成用于承载内容的控件。WPF 的内容对象 为Object类型的,可以是一个string类型的字符串或者是一个Image,甚至可以是另一个控件。(比如在RadioButton 中嵌套一个Image与一个TextBlock)。常见的继承自ButtonBase的Button,RadioButton,CheckBox,ToggleButton以及TextBlock,TextBox等都是内容控件,都可以通过重写它的ControlTemplate 来重写控件结构。本文阐述了如果定义一个共有的Button 样式。(高手可以忽略不看,写给有点WPF基础的同学)
1:定义Button样式
先看效果图:
图中显示了3个button按钮,能想象它们用的是同一个style么?而且还可以设置边框颜色,粗细以及背景色。如果放在基于GDI+的winform控件上,恐怕设计起来会很费劲。不过多废话,贴代码。
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinWidth="500" MinHeight="500" > <Window.Resources> <Style TargetType="{x:Type Button}"> <Setter Property="MinWidth" Value="100"></Setter> <Setter Property="MinHeight" Value="50"></Setter> <Setter Property="Cursor" Value="Hand"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border BorderThickness="3" BorderBrush="DarkViolet" CornerRadius="3" Background="{TemplateBinding Background}" Margin="{TemplateBinding Margin}" Padding="{TemplateBinding Padding}" MinWidth="{TemplateBinding MinWidth}" MinHeight="{TemplateBinding MinHeight}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> <TextBlock Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" Text="{TemplateBinding Content}"></TextBlock> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <UniformGrid Columns="1"> <Button Content="button1" FontSize="15" FontFamily="宋体" Margin="10" Padding="30,10" Background="Red" HorizontalAlignment="Center" VerticalAlignment="Center"> </Button> <Button Content="button2" FontSize="20" FontFamily="宋体" Margin="10" Padding="40,20" Background="Yellow" HorizontalAlignment="Center" VerticalAlignment="Center"> </Button> <Button Content="button3" FontSize="25" FontFamily="宋体" Margin="10" Padding="80,40" Background="Gray" HorizontalAlignment="Center" VerticalAlignment="Center"> </Button> </UniformGrid> </Grid></Window>
可以看出 由Border(在System.Windows.Controls命名空间下) 作为顶级容器以及包含其它子容器构成。
回到Style样式,Style 定义由 Setter 定义一组键值对构成。Property指定属性,Value指定具体值。其中的TargetType 指定要应用样式的类型。
自定义控件结构就需要我们修改 Property为Template 的Value,Template 顾名思义 是 模板的意思。对的,我们就是需要重构其模板。ControlTemplate 下包含一个Border,之所以使用border,是因为方便定义边框的样式,当然也可以使用其它容器控件。在Border中的设置的属性元素都不一一解释了。往下看 会看到一个TextBlock控件,显示内容的主要属性设置是Text="{TemplateBinding Content}",可能会对 引号里边的花括号有疑问,这里的花括号表示一个标记扩展,在WPF看来,只要碰到它,WPF编译器都会把它作为一个标记特性,括号里边的TemplateBinding,从字面意思理解是“模板绑定”,个人理解就是从模板绑定过来,也就是Button元素。之后的Content 标记属性 其实就是Button元素的Content属性,这样在Button元素上设定什么样的Content值,就会在模板中显示成什么值。其它的属性设置也是这个道理。
想测试的同学 直接将代码拷贝至一个新的window窗口或者UserControl 都可以查看效果。
以上都基于个人理解。
1 0
- WPF:自定义控件之Button-自定义样式,共用样式
- Wpf Button自定义样式
- 【android自定义控件】button样式自定义<二>
- ExpressionBlend教程:WPF Button自定义样式
- wpf 自定义控件之Button
- WPF DataGrid 控件(自定义样式篇)
- WPF自定义控件与样式(1)
- 自定义Button样式
- android自定义button样式
- 自定义button按钮样式
- Button按钮自定义样式
- 自定义Button样式
- android自定义button样式
- 关于button自定义样式
- android自定义button样式
- android 自定义button样式
- android 自定义 button样式
- android自定义button样式
- MySQL 常用命令与操作
- 利用bootstrap中tooltip的使用,弹出微信二维码
- 微信涨粉加人100种方法完整版
- MVC Razor语法的一点研究(关于 @Html.ValidationSummary("",new { @class = "text-danger" }))
- iOS控件之UITextField篇
- WPF:自定义控件之Button-自定义样式,共用样式
- python各种数据类型之间的转换
- Roman to Integer
- java中Scanner输入字符串和BufferedReader输入字符串的区别
- svn安装 启动svn服务客户端访问等
- 基于servlet的 考勤管理系统 所遇到的问题
- 解决TextView排版自动换行问题
- 锁屏上显示Activity
- sharepoint client 端检验List是否已经存在的方法。