原文:WPF 构建无外观(Lookless)控件

构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步

  1. 1、继承自System.Windows.Controls.Control
    2、设置DefaultStyleKeyProperty
    3、实现控件功能
  2. 4、定义默认Sytle
  3. 5、添加ThemeInfo
    我借用一个时钟的控件例子,讲解以下每一个步骤
  4. 第1步 继承自System.Windows.Controls.Control

 

我们的自定义控件继承自System.Windows.Controls.Control,如果有更特别的控件,也可以继承自更复杂的控件。

类声明

   1: public class Clock : Control

   2: {

   3: }

 

第2步 设置DefaultStyleKeyProperty

 

无外观的控件需要在静态构造函数中设置DefaultStyleKeyProperty,这样它会去在themes/generic.xaml的文件获取默认的样式。

   1: static Clock()

   2: {

   3:     DefaultStyleKeyProperty.OverrideMetadata(typeof(Clock), new FrameworkPropertyMetadata(typeof(Clock)));

   4: }

第3步 实现控件功能

这一步没什么特别的,就是内部有个DateTime类型和一个DispatcherTimer,完成一个时钟的功能

   1: public class Clock : Control

   2: {

   3:     private DispatcherTimer timer;

   4:  

   5:     static Clock()

   6:     {

   7:         DefaultStyleKeyProperty.OverrideMetadata(typeof(Clock), new FrameworkPropertyMetadata(typeof(Clock)));

   8:     }

   9:  

  10:     protected override void OnInitialized(EventArgs e)

  11:     {

  12:         base.OnInitialized(e);

  13:  

  14:         UpdateDateTime();

  15:  

  16:         timer = new DispatcherTimer();

  17:         timer.Interval = TimeSpan.FromMilliseconds(1000 - DateTime.Now.Millisecond);

  18:         timer.Tick += new EventHandler(Timer_Tick);

  19:         timer.Start();

  20:     }

  21:  

  22:     private void Timer_Tick(object sender, EventArgs e)

  23:     {

  24:         UpdateDateTime();

  25:  

  26:         timer.Interval = TimeSpan.FromMilliseconds(1000 - DateTime.Now.Millisecond);

  27:         timer.Start();

  28:     }

  29:  

  30:     private void UpdateDateTime()

  31:     {

  32:         this.DateTime = System.DateTime.Now;

  33:     }

  34:  

  35:     public DateTime DateTime

  36:     {

  37:         get

  38:         {

  39:             return (DateTime)GetValue(DateTimeProperty);

  40:         }

  41:         private set

  42:         {

  43:             SetValue(DateTimeProperty, value);

  44:         }

  45:     }

  46:  

  47:     public static DependencyProperty DateTimeProperty = DependencyProperty.Register(

  48:             "DateTime",

  49:             typeof(DateTime),

  50:             typeof(Clock),

  51:             new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnDateTimeInvalidated)));

  52:  

  53:     public static readonly RoutedEvent DateTimeChangedEvent =

  54:         EventManager.RegisterRoutedEvent("DateTimeChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<DateTime>), typeof(Clock));

  55:  

  56:     protected virtual void OnDateTimeChanged(DateTime oldValue, DateTime newValue)

  57:     {

  58:         RoutedPropertyChangedEventArgs<DateTime> args = new RoutedPropertyChangedEventArgs<DateTime>(oldValue, newValue);

  59:         args.RoutedEvent = Clock.DateTimeChangedEvent;

  60:         RaiseEvent(args);

  61:     }

  62:  

  63:     private static void OnDateTimeInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)

  64:     {

  65:         Clock clock = (Clock)d;

  66:  

  67:         DateTime oldValue = (DateTime)e.OldValue;

  68:         DateTime newValue = (DateTime)e.NewValue;

  69:  

  70:         clock.OnDateTimeChanged(oldValue, newValue);

  71:     }

  72: }

 

第4步 定义默认Sytle

 

如上所述,我们还需要一个默认的Template,它写在themes/generic.xaml文件中,这个文件可能需要自己创建

   1: <ResourceDictionary

   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   4:     xmlns:local="clr-namespace:CustomControlLibrary"

   5:     >

   6:  

   7:     <Style TargetType="{x:Type local:Clock}">

   8:         <Setter Property="Template">

   9:             <Setter.Value>

  10:                 <ControlTemplate TargetType="{x:Type local:Clock}">

  11:                     <TextBlock Text="{Binding Path=DateTime, RelativeSource={RelativeSource TemplatedParent}}" />

  12:                 </ControlTemplate>

  13:             </Setter.Value>

  14:         </Setter>

  15:     </Style>

  16: </ResourceDictionary>

这里要注意TargetType的使用。到这里控件还是不能找到默认的模板,还需要下面的最后一步

 

  1. 第5步 添加ThemeInfo

 

最后还需要在AssemblyInfo.cs文件中加入ThemeInfo信息,在文件中加入以下特性

   1: [assembly: ThemeInfo(

   2:     ResourceDictionaryLocation.None,

   3:     ResourceDictionaryLocation.SourceAssembly

   4: )]

 

  1. 附注

 

如果需要在模板中使用特定的元素名,来或许模板中的某个特定控件,还有几个地方需要注意。

1、模板内的Name最好以PART开头,好的名称类似于PART_TextBox

2、获取Template中控件的代码,需要重载OnApplyTemplate()方法,可以使用GetTemplateChild方法获取,如下
   1: public override void OnApplyTemplate()

   2: {

   3:     base.OnApplyTemplate();

   4:  

   5:     //从模板中获取名称为PART_PresentationTextBox的TextBox

   6:     _presentationTextBox = GetTemplateChild("PART_PresentationTextBox") as TextBox;

   7:     if(_presentationTextBox != null)

   8:     {

   9:         //对_presentationTextBox进行操作

  10:     }

  11: }

3、在控件类添加TemplatePart

   1: [TemplatePart(Name = "PART_PresentationTextBox", Type = typeof(TextBox))]

   2: public class TimeSpanPicker:Control

这个内容可以参考https://gitcafe.com/atskyline/WPFTimeSpanPickerControl

  1. 参考资料

 

http://www.codeproject.com/Articles/14340/Creating-a-look-less-custom-control-in-WPF

http://www.codeproject.com/Articles/35444/Defining-the-Default-Style-for-a-Lookless-Control

WPF 构建无外观(Lookless)控件的更多相关文章

  1. [转]WPF 构建无外观(Lookless)控件

    构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步 1.继承自System.Windows.Controls.Control 2.设置DefaultStyleKeyPropert ...

  2. 【WPF学习】第六十四章 构建基本的用户控件

    创建一个简单用户控件是开始自定义控件的好方法.本章主要介绍创建一个基本的颜色拾取器.接下来分析如何将这个控件分解成功能更强大的基于模板的控件. 创建基本的颜色拾取器很容易.然而,创建自定义颜色拾取器仍 ...

  3. WPF Timeline简易时间轴控件的实现

    原文:WPF Timeline简易时间轴控件的实现 效果图: 由于整个控件是实现之后才写的教程,因此这里记录的代码是最终实现后的,前后会引用到其他的一些依赖属性或者代码,需要阅读整篇文章. 1.确定T ...

  4. .Net中使用无闪刷新控件时提示框不显示

    今天做提示框的时候一直不显示,让我郁闷好久,晚上吃饭的时候问了同事一下,他给了一个思路, 他说可能是因为由于页面中的无闪刷新导致的结果:百度了一下真找到了解决方法 在页面中存在无闪刷新控件的时候提示框 ...

  5. WPF自定义控件(一)の控件分类

    一.什么是控件(Controls) 控件是指对数据和方法的封装.控件可以有自己的属性和方法,其中属性是控件数据的简单访问者,方法则是控件的一些简单而可见的功能.控件创建过程包括设计.开发.调试(就是所 ...

  6. 基于 WPF 平台的 ActiveReports Viewer控件

    ActiveReports 报表控件致力于为组织和个人提供最出色的报表解决方案,多年来ActiveReports已经提供了 Windows Forms.Web.Silverlight和Flash平台的 ...

  7. WPF不同线程之间的控件的访问

    原文:WPF不同线程之间的控件的访问 WPF不同线程之间的控件是不同访问的,为了能够访问其他线程之间的控件,需要用Dispatcher.Invoke执行一个新的活动即可. 例如: public voi ...

  8. WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)

    Windows Community Toolkit 再次更新到 5.0.以前可以在 WPF 中使用有限的 UWP 控件,而现在有了 WindowsXamlHost,则可以使用更多 UWP 原生控件了. ...

  9. WPF 动画:同为控件不同命 - 简书

    原文:WPF 动画:同为控件不同命 - 简书 1. 及格与优秀 读大学的时候,有一门课的作业是用 PPT 展示. 但是我们很多同学都把 PPT 当做 Word 来用,就单纯地往里面堆文字. 大家都单纯 ...

随机推荐

  1. Jmeter启动报错:unable to access jarfile ApacheJmeter.jar error 原因:下载的src包没有这个jar包,需下载binary包

    安装好jdk并配置了环境变量,下载Jmeter包解压启动jemter.bat提示 unable to access jarfile ApacheJmeter.jar error 原因: 从官网 htt ...

  2. OC block声明和使用

    #import "Button.h" typedef int (^MySum) (int, int); void test() { // 定义了一个block,这个block返回值 ...

  3. Python map/reduce/filter/sorted函数以及匿名函数

    1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ...

  4. POJ 2704 Pascal's Travels 【DFS记忆化搜索】

    题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  5. python time模块计算程序耗时

    import time start = time.clock() end = time.clock() consume_time = end - start

  6. 【luogu P1137 旅行计划】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1137 topsort + DP #include <queue> #include <cs ...

  7. SPFA 全面讲解

    SPFA全面讲解 --最短路高效算法 标签: 最短路 简介:SPFA 是1994年在西安交通大学段凡丁同学所提出,是将Dijsktra以及Bellman-Ford两种最短路算法完美结合的一个算法,效率 ...

  8. JavaScript面向对象编程之创建对象

    参考资料依旧<JavaScript高级程序设计>,不得不说这本书写的太好了,讲的极为清晰凝练,好书! 先给出重点笔记,好好理解下面的三条笔记,每一句话都很重要: 1.实例的指针仅指向原型, ...

  9. 移动端判断微信浏览器安卓浏览器和ios浏览器

    $(function(){ var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf ...

  10. 使用 Cordova(PhoneGap)构建Android程序

    移动终端开发随着智能手机的普及变得越来越热,什么项目都想移动一把,但反观要去学这些各个终端的控件使用,实属不易,又特别是每个终端的控件及所用语言都各不相同,使得这种学习变得更加困难. 有没有一种简单的 ...