Implement user-defined controls

May include but is not limited to: deciding whether to use a user/composite, extended, or custom control; creating a user/composite control; extending from an existing control

This is another area where the WPF control model and the ASP.NET control model overlaps closely. As you can remember (it was posted on this blog) in ASP.NET there are custom controls (they can be installed into the toolbox and dragged from there to the form, and they are the basic unit of encapsulation) and user controls, which are made for a single application, by composing some existing controls together. This stands for WPF as well.

MS exams aren’t very good at custom controls (I think they are too complicated to test). This objective has it written too that you don’t have to implement a custom control by writing code. Huh, we got our asses saved, no code writing! Just stick close to XAML and no harm can come to you. So we have to decide when we will use which type of controls. I think those questions contain their answers as well – when you need something duct-taped together, write a user defined control. If you need some robust piece of functionality – write a custom control. If you need just one more thing to add (a blinking tooltip) just extend the ToolTip class – these are pretty straightforward.

Now how would you write a user control? Well, I personally would start Visual Studio, create a new project and select the project type WPF User Control Library. Here I would have a XAML file and a code-behind class, which states that I’m currently writing a new class which has UserControl as its base class which in turn inherits from ContentControl. Just like we created a new WPF application!

What I would do is writing some XAML code to add the controls I needed – after all, we are composing a control from existing ones. Then I’d write some dependency properties, and if I would need it, then I’d throw in some routed events.

After I’d be done with all of these (of course I’d write unit tests constantly and with great enthusiasm) I’d compile my project and reference it from a WPF application. To make it work, I’d add a reference to the control’s namespace and assembly on the windows (or in the application) I planned to use it. Adding a reference is easy, just like this:

<Window …
  <uc1:MyShinyNewControl … />

It’s just my opinion; you are free to do it other ways as well. And how would I extend an existing control? Maybe I’d subclass it – with the dependency properties/routed events I’d need.


Tags: ,

One response to “Implement user-defined controls”

  1. Ankit Singh says :

    Very informative post. It’s really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about Populate Grid Control From XML Document Easily, for more details of this post check out this link…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: