I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering, and Programming.
Fahrenheit Marketing is a top-dog Austin Web Design firm offering a complete portfolio of online services.
Regular visitors can remember that I had written some posts about writing a custom workflow activity to clarify things. I got good feedbacks for those posts so decided to write a few posts about similar stuff in WPF about building a custom WPF control. So this post and some future posts will cover the topic of different ways of customization in WPF and their different aspects.
Building a custom control in WPF isn't as common as building a custom control in other UI engines and technologies because there are several ways to customize a built-in control for your needs rather than building a custom control. All built-in controls are customizable in different ways (using appearance attributes) and this is an advantage of WPF. On the other hand it's possible to create user controls and apply them in applications to create a new controls based on existing controls.
But however there are some cases that you need to build a custom control. One of most common cases is when you want to something new to code functionality for a control for your needs. In this case you need to derive from your base control and add extra functionality to it.
Writing a custom control consists of some steps that I want to cover but here first I prefer to give a background about basics.
Building a custom WPF control is nothing more than writing a class and inheriting this class from a base class that is provided in WPF.
First step in development is to choose the most appropriate base class for your needs. There are several base classes in WPF hierarchy that can be chosen for base class but you should choose the most appropriate one to have the best result.
For example in a hierarchy manner, FrameworkElement is derived from UIElement and UIElement is derived from System.Windows.Media.Visual and this hierarchy follows to upper level to reach to root. On the other side System.Windows.Controls.Control is derived from FrameworkElement and ContentControl is derived from Control base class. This ContentControl is a base class for some other controls that you use in WPF like HeaderedContentControl. Below is a diagram of this hierarchy that I took from here (you don't see the upper levels).
Sometimes you need to deal with lower level APIs and have to choose something in leafs or lower levels but sometimes you need to work directly with some basic structures and have to choose an upper level base class.
After creating a Class Library project and adding some WPF references to it I create a new class, MyControl, to implement my custom control. For example if I'm interested to write a custom content control then I can choose ContentControl base class for my control.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace WPFCustomControl
{
public class MyControl : ContentControl
{
}
}
Or I can create an item based control and derive it from ItemsControl.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace WPFCustomControl
{
public class MyControl : ItemsControl
{
}
}
The important key is to choose the right base class. Choosing a base class from upper levels when you can choose something in lower levels has its own problems. For example you miss some built-in functionality that is ready for use in WPF and may need to write it yourself. In opposite direction choosing a base class from lower levels when you have to choose one from upper levels has other problems because you won't be able to write your codes and accomplish to what you have in mind and later need to change your base class which may cause to change other codes as well.
Alright, it's enough for this post. In next posts I'll talk about ways to add your properties, events and ... to your custom controls.
Fahrenheit Marketing is your resource for Search Engine Optimization in Austin.
DotNetKicks.com
Apr 21, 2007 12:12 PM
#
Keyvan Nayyeri
Apr 28, 2007 11:56 AM
#
Keyvan Nayyeri
May 08, 2007 11:13 AM
#
Keyvan Nayyeri
May 15, 2007 11:08 AM
#
majkez
Jun 06, 2007 4:59 AM
#
adi
Apr 10, 2008 3:23 AM
#
"In next posts I'll talk about ways to add your properties, events and ..." Are there any other posts ?
Emad Rangchi
Jun 26, 2008 5:58 AM
#
Hi
Nice work, But how I can add property to control without inherit from base control like method extention
your twonsman
ArunManick
Sep 10, 2008 9:02 AM
#
Yah,after i read this post i have known about how to create custom control...Thanks...where can i get regarding custom control? if anybody knows pls inform me at arunmanickaraja@gmail.com
brian tan
Oct 10, 2008 4:05 AM
#
How to overraid a event of the control?
Mehmet AKTURK
Apr 09, 2010 7:30 AM
#
And I can choose this component for property of anothor visiual control.
How can i create an wpf control like this.
For exaple;
binder: invisual wpf control
MyGrid: visual wpf control and one of properties is binder.
in design time i should add a binder to window but it must not visible and i should add a MyGrid to window and then
i should choose the binder at binder property of the mygrid in properties pane.
is it possible? Thank you...
Leave a Comment