WPF 构建无外观(Lookless)控件
构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步
- 1、继承自System.Windows.Controls.Control
2、设置DefaultStyleKeyProperty
3、实现控件功能 - 4、定义默认Sytle
- 5、添加ThemeInfo
我借用一个时钟的控件例子,讲解以下每一个步骤 第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的使用。到这里控件还是不能找到默认的模板,还需要下面的最后一步
第5步 添加ThemeInfo
最后还需要在AssemblyInfo.cs文件中加入ThemeInfo信息,在文件中加入以下特性
1: [assembly: ThemeInfo(
2: ResourceDictionaryLocation.None,
3: ResourceDictionaryLocation.SourceAssembly
4: )]
附注
如果需要在模板中使用特定的元素名,来或许模板中的某个特定控件,还有几个地方需要注意。
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
参考资料
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)控件的更多相关文章
- [转]WPF 构建无外观(Lookless)控件
构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步 1.继承自System.Windows.Controls.Control 2.设置DefaultStyleKeyPropert ...
- 【WPF学习】第六十四章 构建基本的用户控件
创建一个简单用户控件是开始自定义控件的好方法.本章主要介绍创建一个基本的颜色拾取器.接下来分析如何将这个控件分解成功能更强大的基于模板的控件. 创建基本的颜色拾取器很容易.然而,创建自定义颜色拾取器仍 ...
- WPF Timeline简易时间轴控件的实现
原文:WPF Timeline简易时间轴控件的实现 效果图: 由于整个控件是实现之后才写的教程,因此这里记录的代码是最终实现后的,前后会引用到其他的一些依赖属性或者代码,需要阅读整篇文章. 1.确定T ...
- .Net中使用无闪刷新控件时提示框不显示
今天做提示框的时候一直不显示,让我郁闷好久,晚上吃饭的时候问了同事一下,他给了一个思路, 他说可能是因为由于页面中的无闪刷新导致的结果:百度了一下真找到了解决方法 在页面中存在无闪刷新控件的时候提示框 ...
- WPF自定义控件(一)の控件分类
一.什么是控件(Controls) 控件是指对数据和方法的封装.控件可以有自己的属性和方法,其中属性是控件数据的简单访问者,方法则是控件的一些简单而可见的功能.控件创建过程包括设计.开发.调试(就是所 ...
- 基于 WPF 平台的 ActiveReports Viewer控件
ActiveReports 报表控件致力于为组织和个人提供最出色的报表解决方案,多年来ActiveReports已经提供了 Windows Forms.Web.Silverlight和Flash平台的 ...
- WPF不同线程之间的控件的访问
原文:WPF不同线程之间的控件的访问 WPF不同线程之间的控件是不同访问的,为了能够访问其他线程之间的控件,需要用Dispatcher.Invoke执行一个新的活动即可. 例如: public voi ...
- WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)
Windows Community Toolkit 再次更新到 5.0.以前可以在 WPF 中使用有限的 UWP 控件,而现在有了 WindowsXamlHost,则可以使用更多 UWP 原生控件了. ...
- WPF 动画:同为控件不同命 - 简书
原文:WPF 动画:同为控件不同命 - 简书 1. 及格与优秀 读大学的时候,有一门课的作业是用 PPT 展示. 但是我们很多同学都把 PPT 当做 Word 来用,就单纯地往里面堆文字. 大家都单纯 ...
随机推荐
- fiddler和charles
(4)学习浏览器代理和手机网络抓包,涉及到了fiddler和charles,
- IOS VFL语言(页面布局)
● 什么是VFL语言 ● VFL全称是Visual Format Language,翻译过来是“可视化格式语言” ● VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 VFL ...
- 初学DLX
前言 \(DLX\),全称\(Dancing\ Links\ X\),即舞蹈链算法. 这是一个十分高效且实用的算法,它主要用于求出精确覆盖问题的一组解.(貌似重复覆盖问题也可以,但我不会\(2333\ ...
- 【转】Android Fragment 真正的完全解析(上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fra ...
- 字符ASCII转换
实现效果: 关键知识: 实现代码: private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != stri ...
- LinQ 简介
LinQ是3.0新加的语法.用起来比较方便,我们可以使用较简单的方法来过滤数据和处理数据. 使用场景: 可以看到LINQ使用场景还是很多的.现在写项目基本都会用到. 在出现委托之前,我们来查找对象 在 ...
- Extjs 4.2 panel 添加 click 事件及右键菜单
listeners: { render: function(c) { c.body.on('click', function() { //TODO 添加点击事件功能 }); c.body.on('co ...
- oracle 的分页查询与mysql 的分页查询
oracle 的分页查询: select * from (select o.*,rownum rn from Persons o) where rn >40 and rn <=60 : ...
- 极光推送小结 - iOS
此次即友盟分享小结(友盟分享小结 - iOS)之后对推送也进行了一版优化.此次分享内容依然基于已经成功集成 SDK 后 code 层级部分. 注:此次分享基于 SDK 3.1.0,若版本相差较大,仅供 ...
- zepto 基础知识(4)
61.prev prev() 类型:collection prev(selector) 类型:collection 获取对相集合中每一个元素的钱一个兄弟节点,通过选择器来进行过滤 62.prev pr ...