I have always found that there isn’t enough documentation about Custom Control development in WPF. Most of the information is segregated into articles, blog entries and posts on the WPF Forums. In order to save the trouble for other fellow WPF developers I intend to document all my findings at one place: this blog. To start with, there are some articles which are a must read for all control developers:

  1. Control Authoring Overview
  2. Kevin Moore’s article on ColorPicker control
  3. Creating lookless control
  4. Control Styling Guidelines [Update: Added link]

Below are some of the practices which I found useful when creating my own custom controls.

Event-handling practices:

  • Use RoutedEvents, Commands and CommandManager
  • Use TemplatePartAttribute
  • Name the controls that are used inside the custom control with something like “PART_SubControlName”. The PART_ prefix is a convention.
  • This is useful if you want your control to be styled using a Designer. I am not aware of the exact semantics of how this is used.
  • Specify the TemplatePartAttribute for the custom control class
  • Override OnApplyTemplate() and check for existence of the subcontrols in use. Check using the GetTemplateChild() method. If a part by that name is absent, throw an exception.
  • Attach event handlers on the sub control

Theming practices:

  • Create a ResourceDictionary called generic.xaml. Create a folder named themes in your library and put generic.xaml inside that.
  • Organize multiple resources using MergedDictionaries [Article]
  • Override the DefaultStyleKeyProperty inside the custom controls static constructor.
  • Use ComponentResourceKey on specific resources used within the custom control. It is used to embed resources as part of the control.

I will make more posts as I learn about other practices. I am open to feedback from other developers. In future posts I should have more information about Databinding, Localizationpractices.

Recommended Practices for WPF Custom Control Developers的更多相关文章

  1. ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同

    说来惭愧,接触WPF这么长时间了,今天在写自定义控件时遇到一个问题:运行界面中并没有显示自定义控件,经调试发现原来没有加载Themes中的Generic.xaml. 可是为什么在其他solution中 ...

  2. wpf custom control

    最近在做WPF,记录一下自定义控件的制作过程,源码请点击:源码. 1.目标 实现一个如图所示的可增减的数字框: 2.先画Template 可以在Generic.xaml中画,也可以用MergedDic ...

  3. [WPF] 将普通的Library工程,改造成WPF Custom Control 的Library

    1. 添加References PresentationCore PresentationFramework System.Xaml WindowsBase2. 修改AssemblyInfo.xsus ...

  4. Hosting custom WPF calendar control in AX 2012

    原作者: https://community.dynamics.com/ax/b/axilicious/archive/2013/05/20/hosting-custom-wpf-calendar-c ...

  5. Writing a Reusable Custom Control in WPF

    In my previous post, I have already defined how you can inherit from an existing control and define ...

  6. WPF中的Generic.xaml, theme以及custom control

    原文:WPF中的Generic.xaml, theme以及custom control 在Visual Studio中创建自定义控件时,所有控件都将添加到/Themes/Generic.xaml. 最 ...

  7. .NET Core学习笔记(2)—— WPF使用UWP Custom Control

    自.NET Core 3.0开始,某软加入了对WPF的支持.同时对XAML Islands也做了进一步加强.在.NET Core 3.0之前,我们只能在WPF程序中,通过两种方式有限制地使用Stand ...

  8. Tutorial: WPF User Control for AX2012

    原作者: https://community.dynamics.com/ax/b/goshoom/archive/2011/10/06/tutorial-wpf-user-control-for-ax ...

  9. WinForm中Component Class、User Control及Custom Control的区别和使用建议

    reference: http://blog.csdn.net/redstonehe/article/details/1536549 .NET Framework 为您提供了开发和实现新控件的能力.除 ...

随机推荐

  1. Cocos2d-x 源代码分析 : Scheduler(定时器) 源代码分析

    源代码版本号 3.1r,转载请注明 我也最终不out了,開始看3.x的源代码了.此时此刻的心情仅仅能是wtf! !!!!!!! !.只是也最终告别CC时代了. cocos2d-x 源代码分析文件夹 h ...

  2. 看了一下unity5.6的新功能 以及Timeline

    3月31日unity5.6发布,然而timeline(前sequence模块)被delay到unity 2017.上个星期官方又发布了unity 2017的beta版本 抽空看了下 (unity5.6 ...

  3. Binlog中最容易踩到的坑

    MySQL高可用架构中,主库复制是非常常见的一种. 当主库宕机后,可以提升一个从库作为新的主库,保证服务可用性:同时可以通过扩展从库,提高整个集群的QPS. 在主从复制架构下,MySQL通过binlo ...

  4. Replace 在动态sql中的实现

    set @stsqlReplace=' update ChgCfm set cfmdate=replace(cfmdate,'''''''',''''), cfmstatu=replace(cfmst ...

  5. (转)Maven学习-处理资源文件

    转自:http://www.cnblogs.com/now-fighting/p/4888343.html 在前面两篇文章中,我们学习了Maven的基本使用方式和Maven项目的标准目录结构.接下来, ...

  6. 使用终端执行.app程序 for mac

    背景:打开Eclipse闪退,需要查看错误日志 问题:使用Terminal执行 ./Eclipse.app时,提示“no such file or directory” 解决方案:执行./Applic ...

  7. Tcpdump 的用法

    1.抓取回环网口的包:tcpdump -i lo 2.防止包截断:tcpdump -s0 3.以数字显示主机及端口:tcpdump -n 第一种是关于类型的关键字,主要包括host,net,port, ...

  8. Lintcode: Majority Number 解题报告

    Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...

  9. 【机器学习】粗糙集(Rough Set Approach)

    粗糙集理论是一种研究不精确,不确定性知识的数学工具. 粗糙集理论的知识表达方式一般采用信息表或称为信息系统的形式,它可以表现为四元有序组K=(U,A,V,P).其中U为对象的全体,即论域:A是属性全体 ...

  10. 如何在Windows环境下模拟丢包

    [本文出自天外归云的博客园] Q&A Question: How to simulate packet loss on Windows? Answer: Clumsy, an utility ...