WPF之自定义控件
1.先定义画刷,一般存为资源字典
格式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--定义 画刷--> <!-- 字体--> <!-- 等 --> </ResourceDictionary>
定义SolidBrush:
<SolidColorBrush x:Key="WindowBackground" Color="#007ACB" /> <SolidColorBrush x:Key="WindowBorderBrush" Color="Transparent" /> <SolidColorBrush x:key="WindowForeground" Color="White" />
定义LinearGradientBrush:
<LinearGradientBrush x:Key="CaptionBackground" StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Color="#571457" Offset="0"/> <GradientStop Color="#6A196A" Offset="1"/> </LinearGradientBrush>
设置边框效果:
1<DropShadowEffect x:Key="DefaultDropShadow" Color="Black"
BlurRadius="5" ShadowDeph="2" Direction="31" Opacity="0.6"/>
定义字体:
<FontFamily x:Key="FontFamily">Microsoft YaHei</FontFamily>
定义数值和字符串:
<sys:Double x:Key="FontSize">13</sys:Double> <sys:Double x:Key="WatermarkOpacity">0.4</sys:Double> <sys:String x:Key="DateFormat">yyyy年MM月dd日</sys:String> <sys:String x:Key="DateTimeFormat">yyyy/MM/dd HH:mm:ss</sys:String>
2.定义样式:
格式:注意需要引用当前类库的命名空间
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:SLT.Controls" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!----> <!--样式--> <!----> </ResourceDictionary>
TargetType指定样式的应用对象
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
<Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
<Setter Property="FontSize" Value="{StaticResource FontSize}"/>
</Style>
带ControlTemplate的样式:
<Style TargetType="{x:Type ToolTip}">
<Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
<Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
<Setter Property="FontSize" Value="{StaticResource FontSize}"/>
<Setter Property="Background" Value="{StaticResource HeaderBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource FocusBorderBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<Border CornerRadius="2" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter Margin="8,5,8,5"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
3.添加MergedDictionaries:
所有的样式文件需要添加说明,格式如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<!---->
<!--此处添加样式文件-->
<!---->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
样式文件说明需要说清文件的具体位置,如下:
1 <ResourceDictionary Source="pack://application:,,,/SLT.Controls;component/Control/HighTextBlock.xaml" />
WPF之自定义控件的更多相关文章
- 在WPF中自定义控件
一, 不一定需要自定义控件在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费周章,因为控件可以嵌套使用以及可以为控件外观打造一套新的样 ...
- 在WPF中自定义控件(3) CustomControl (上)
原文:在WPF中自定义控件(3) CustomControl (上) 在WPF中自定义控件(3) CustomControl (上) 周银辉 ...
- 在WPF中自定义控件(3) CustomControl (下)
原文:在WPF中自定义控件(3) CustomControl (下) 在WPF中自定义控件(3) CustomControl (下) ...
- 在WPF中自定义控件(1)
原文:在WPF中自定义控件(1) 在WPF中自定义控件(1):概述 周银辉一, 不一定需要自定 ...
- 在WPF中自定义控件(2) UserControl
原文:在WPF中自定义控件(2) UserControl 在WPF中自定义控件(2) UserControl ...
- [转]在WPF中自定义控件 UserControl
在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...
- WPF 杂谈——自定义控件
如果只是使用现有的WPF控件的话,是很难满足当前社会多复杂的业务.所以用户自己订制一系列控件也是一种不可避免的情势.WPF在控制方面分为俩种:用户控件和自定义控件.相信看过前面章节的就明白他们俩者之间 ...
- WPF创建自定义控件并运用
此项目源码:https://github.com/lizhiqiang0204/WpfCustomControlLibrary1 首先创建自定义控件库项目 项目名称命名为:WpfCustomContr ...
- WPF设计自定义控件
在实际工作中,WPF提供的控件并不能完全满足不同的设计需求.这时,需要我们设计自定义控件. 这里LZ总结一些自己的思路,特性如下: Coupling UITemplate Behaviour Func ...
- WPF 创建自定义控件及自定义事件
1 创建自定义控件及自定义事件 /// <summary> /// 演示用的自定义控件 /// </summary> public class ExtButton : Butt ...
随机推荐
- 基于centos的lnmp搭建
部署lnmp环境 安装配置mariadb配置php配置phpmyadmin配置php-fpm配置nginx配置测试 LNMP(linux,nginx,mariadb,php)部署,以下默认在root权 ...
- PRINCE2特征(三)
提到不确定性,可能很多从事项目管理相关工作的人都会感同身受,一系列临时性问题,比如:变更.延期.调整.计划赶不上变化.团队调整等的出现,都是项目中再正常不过的内容.但正常不等于合理,我们还是要去思考一 ...
- C#-WebForm-纯HTML提交方式
此方法常用于 纯.html页面向数据库添加数据 在.aspx网页中可以通过传值的方式提交信息,如何在.html网页中提交数据? 提交数据需要在同一个 form 中,用到两个属性:action.meth ...
- 附加属性出现Failed to assign to property的问题
找了半天资料,最后发现把保护附加属性的类加上public就行了
- 编译OpenCV文档
概述 使用OpenCV的过程中经常查看文档,每次都去官网查看,不过国内访问速度很慢,有一份本地的文档就好了.本文列出了在Linux(Fedora)系统上从OpenCV源码编译出documentatio ...
- div+css:div中图片垂直居中
div中图片垂直居中 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...
- 添加自编译的apache为linux系统服务
步骤1添加服务脚本 #cp /app/apache2/bin/apachectl /etc/rc.d/init.d/httpd #ln -s /etc/rc.d/init.d/httpd /etc/r ...
- Chrome 开发工具之Network
经常会听到比如"为什么我的js代码没执行啊?","我明明发送了请求,为什么反应?","我这个网站怎么加载的这么慢?"这类的问题,那么问题既然 ...
- 人工智能之一《tensorflow》
http://wiki.jikexueyuan.com/project/tensorflow-zh/
- css权威指南学习笔记
2016-08-03 1,继承 一般大多数框模型属性都是不能继承的.如:padding .margin .border .background 都不能继承. 继承值,完全没有特殊性.(就是优先级最低) ...