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. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  2. input type="datetime-local" 时placeholder不显示

    一个坑,input的type="datetime-local" 时,电脑上会显示提示,如图 <input type="datetime-local" na ...

  3. apache.commons.compress 压缩,解压

    最近在一个前辈的指引下,开始研究apache.commons.都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹 package my.test; import java.io.Buf ...

  4. HTML 全局属性

    http://www.w3school.com.cn/tags/html_ref_standardattributes.asp

  5. div+css:div中图片垂直居中

    div中图片垂直居中 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  6. Linux下几款好用的录屏软件

    最近需要在Linux环境下录制一段视频,自己的Linux是LinuxMint Xfce 18,网上搜了一圈发现都不太顺手.尤其是VLC,感觉不是很易用,幸好最后在Linux自带的软件管理器找到了两个不 ...

  7. 解决Windows版Git出现templates not found的问题

    环境: Win10 x64 Git windows客户端(下载自 https://git-scm.com/) SourceTree 1.9.6.1(使用系统安装的Git,而非SourceTree内嵌的 ...

  8. rmarkdown教程

    https://github.com/yihui/r-ninja/blob/master/11-auto-report.md http://rpubs.com/about/getting-starte ...

  9. 高性能异步图片加载器的JS库:lazysizes

    <script src="lazysizes.min.js" async=""></script> 使用示例 <!-- non-r ...

  10. iOS 动态化

    来自bang's blog http://blog.cnbang.net/tech/3286/ 问题 在开发模式上,web 的方式是比较先进的,有各种优点,包括跨平台/UI开发效率高,最重要的是可以时 ...