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 ...
随机推荐
- iOS App上架流程(2016详细版)
iOS App上架流程(2016详细版) 原文地址:http://www.jianshu.com/p/b1b77d804254 感谢大神整理的这么详细 一.前言: 作为一名iOSer,把开发出来的Ap ...
- CSS你可能还不知道的一些知识点
一.特殊选择器 1.* 用于匹配任何的标记 2.> 用于指定父子节点关系 3.E + F 毗邻元素选择器,匹配所有紧随E元素之后的同级元素F 4.E ~ F 匹配所有E元素之后的同级元素F 5. ...
- VS2013编译google protobuf 出现问题error C3861: “min”:
问题描述: 今天用vs2013编译protobuf 2.4.1 报错: 错误 3 error C3861: "max": 找不到标识符 f:\google\protobuf\pro ...
- Android基础总结(七)
广播(掌握) 广播的概念 现实:电台通过发送广播发布消息,买个收音机,就能收听 Android:系统在产生某个事件时发送广播,应用程序使用广播接收者接收这个广播,就知道系统产生了什么事件. Andro ...
- Uncaught SyntaxError: Invalid or unexpected token
出现错误的地方:在Jquery中,调用有参数的方法,动态传递参数时报错 出现错误的原因: 动态传递参数的时候,参数中有换行符 错误的解决:参数传递之前,将换行符替换 var temp = model ...
- localForage——轻松实现 Web 离线存储(转)
localStorage 能够让你实现基本的数据存储,但它的速度慢,而且不能处理二进制数据.IndexedDB 和 WebSQL 是异步的,速度快,支持大数据集,但他们的API 使用起来有点复杂.不仅 ...
- js导出excel
function inportEx() { $("#btnEx").text("导出中..."); var fugNumber = "";/ ...
- BZOJ 2424: [HAOI2010]订货
2424: [HAOI2010]订货 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 915 Solved: 639[Submit][Status][ ...
- Non-blocking read on a subprocess.PIPE in python
import sys from subprocess import PIPE, Popen from threading import Thread try: from Queue import Qu ...
- oracle--函数--bai
--1 没有入参的函数.返回字符串 create or replace function get_time return varchar2 as v_time varchar2(20); --声明1个 ...