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之自定义控件的更多相关文章

  1. 在WPF中自定义控件

    一, 不一定需要自定义控件在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费周章,因为控件可以嵌套使用以及可以为控件外观打造一套新的样 ...

  2. 在WPF中自定义控件(3) CustomControl (上)

    原文:在WPF中自定义控件(3) CustomControl (上) 在WPF中自定义控件(3) CustomControl (上)                              周银辉 ...

  3. 在WPF中自定义控件(3) CustomControl (下)

    原文:在WPF中自定义控件(3) CustomControl (下)   在WPF中自定义控件(3) CustomControl (下)                                 ...

  4. 在WPF中自定义控件(1)

    原文:在WPF中自定义控件(1)    在WPF中自定义控件(1):概述                                                   周银辉一, 不一定需要自定 ...

  5. 在WPF中自定义控件(2) UserControl

    原文:在WPF中自定义控件(2) UserControl 在WPF中自定义控件(2) UserControl                                               ...

  6. [转]在WPF中自定义控件 UserControl

    在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...

  7. WPF 杂谈——自定义控件

    如果只是使用现有的WPF控件的话,是很难满足当前社会多复杂的业务.所以用户自己订制一系列控件也是一种不可避免的情势.WPF在控制方面分为俩种:用户控件和自定义控件.相信看过前面章节的就明白他们俩者之间 ...

  8. WPF创建自定义控件并运用

    此项目源码:https://github.com/lizhiqiang0204/WpfCustomControlLibrary1 首先创建自定义控件库项目 项目名称命名为:WpfCustomContr ...

  9. WPF设计自定义控件

    在实际工作中,WPF提供的控件并不能完全满足不同的设计需求.这时,需要我们设计自定义控件. 这里LZ总结一些自己的思路,特性如下: Coupling UITemplate Behaviour Func ...

  10. WPF 创建自定义控件及自定义事件

    1 创建自定义控件及自定义事件 /// <summary> /// 演示用的自定义控件 /// </summary> public class ExtButton : Butt ...

随机推荐

  1. RandHelper

    /// <summary> /// 类说明:RandHelper类,用来实现取随机 /// 编码日期:2015-12-29 /// 编 码 人:里程碑 /// 修改日期:2015-12-2 ...

  2. spring 拦截器

    1.mvc.xml <!-- 自定义拦截链配置 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping p ...

  3. C#设计模式(3)——工厂方法模式

    一.概念:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 二.代码实现 namespace 设计模式之工厂方法模式 { /// <summary&g ...

  4. C# 在数组中判断是否存在某个数组值

    (1) 第一种方法: ,,}; ); // 这里的1就是你要查找的值 ) // 不存在 else // 存在 (2) 第二种方法: string[] strArr = {"a",& ...

  5. linux 让程序在后台运行的几种可靠方法

    我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败.如何让命令提交后不受本地关闭终端窗口/网络断开 ...

  6. 美国usan数据库——PDF提取

    QQ:231469242 原创 单个PDF内容提取 # -*- coding: utf-8 -*- """ io.open() is the preferred, hig ...

  7. 顶级域名和二级域名cookie共享删除和修改

    原文地址: https://segmentfault.com/a/1190000006932934

  8. js笔记

    1.克隆对象 克隆数组: var country=['中国','美国']; var copyCountry=country.slice(0); 克隆对象: var people={sex:'man', ...

  9. Java基础——继承、接口

    一个对象变量(例如,变量e)可以引用多种实际类型的现象被称为多态(polymorphism). 在运行时能够自动地选择调用哪个方法的现象称为动态绑定(dynamic binding). 在Java程序 ...

  10. 6. web前端开发分享-css,js移动篇

    随着移动市场的逐步扩大及相关技术的日趋完善,对前端开发提出了新的岗位要求,在继承前人成果的基础上需要在新的历史条件下有新的创新.移动端的开发,虽然没有IE6众多问题的折磨,但是多平台,多设备的兼容,也 ...