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. Android Studio插件之快速findViewById(butterknife和Android CodeGenerator的使用)

    首先在设置里面的Plugins里面下载安装插件: 安装之后会提示重启, 然后就是怎么使用了: butterknife的使用: 首先在build.gradle(app)里面添加这句话: compile ...

  2. CentOS安装Redis

    wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...

  3. springboot @ConfigurationProperties

    Spring Boot 提供了一种方式 --类型安全的bean,能够根据类型校验和管理application中的bean.继续使用author做例子.配置放在author.properties文件中. ...

  4. smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')解决办法

    raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b'Error: authentica ...

  5. 使用Xmanager访问CentOS远程桌面

    最近在搞Qemu虚拟机相关的项目,需要用到Linux的桌面系统,用Xmanager连接CentOS桌面最方便了. Linux端:CentOS release 6.8 (Final) Windows端: ...

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

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

  7. 关于win7 安装redis的问题

    首先在https://github.com/MSOpenTech/redis/releases下载64位的安装包 到任意盘中 将改名为redis 使用cmd命令 启动redis 进入 redis 目录 ...

  8. 借助JavaScript中的时间函数改变Html中Table边框的颜色

    借助JavaScript中的时间函数改变Html中Table边框的颜色 <html> <head> <meta http-equiv="Content-Type ...

  9. 【转】机器学习教程 十四-利用tensorflow做手写数字识别

    模式识别领域应用机器学习的场景非常多,手写识别就是其中一种,最简单的数字识别是一个多类分类问题,我们借这个多类分类问题来介绍一下google最新开源的tensorflow框架,后面深度学习的内容都会基 ...

  10. 如何提高账户密码存储的安全性——PasswordSalt的使用

    使用 Salt + Hash 将密码加密后再存储进数据库 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的 ...