Recommended Practices for WPF Custom Control Developers

本文详细阐述了WPF自定义控件开发中的一些关键实践,包括事件处理、主题应用、资源组织等,旨在为WPF开发者提供一站式文档。文章精选必读的文章,分享创建无外观控件、控制样式指南,并提出实用的实践技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值