19

I'm working on creating a date/time user control in WPF using C# 2008. My first user control. I'm also using Matthew MacDonald's book, "Pro WPF in C# 2008". In that book he strongly recommended creating a user control using the WPF Custom Control Library project template; so I followed his suggestion. I've finished writing the code which would go into what I think of as the code-behind file. Now I'm ready to write the XAML.

The only problem is, I just discovered there is no corresponding .xaml file? So, I don't get why using a WPF Custom Control Library project is better, or prefered, when writing a user control?

leppie
  • 115,091
  • 17
  • 196
  • 297
Rod
  • 4,107
  • 12
  • 57
  • 81

2 Answers2

30

A user control and a custom control solve two distinctly different problems.

UserControls are meant to compose multiple WPF controls together, in order to make a set of functionality built out of other controls. This is often used to compose a portion of a window or screen in order to organize your development by allowing you to group multiple pieces of functionality into one "control". For example, if you wanted to make a control for editing a User which provided text boxes for first and last name, age, etc., a single UserControl could be dropped onto a Window and bound to a User instance to edit this. (In this case, you're using standard controls, such as TextBox, to "compose" a control for a more complex purpose.)

A CustomControl, however, is meant to be a new single control. This would typically be a replacement for a built-in control (which could not be redone via templating). I've found that the need for CustomControls is actually fairly rare in WPF, since the WPF templating options and attached properties allow you to do nearly anything with standard controls, once you learn them fully.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thank you for the explanation. It seems to me then that what I need to do is use the user control project and not the custom control library project. – Rod Jun 07 '10 at 20:49
  • 5
    This doesn't answer the question. The question was what is the difference between a Custom Control and User Control **Library**. I can take this answer and infer which one I should use, but it still doesn't tell me how the two project templates are different. – Joel McBeth Jul 10 '13 at 13:49
  • Technically I do agree with you @JoelMcBeth but it is still a very useful great post. I tried to give a more complete answer (https://stackoverflow.com/a/70308458/184528) to the same question asked elsewhere. Of course I credited Reed. – cdiggins Dec 10 '21 at 17:47
1

I would also add, if you're intending to inherit from your control, then using a usercontrol is will complicate matters. For example, if create a base usercontrol that has a layout defined in Xaml, WPF framework won't allow you to inherit this control and use Xaml to define the layout for the subclass. In dotnet 3.5 Xaml control can't inherit from another Xaml control

elsayeda73
  • 21
  • 2
  • 1
    You are right. That is why one must favor Composition over inheritance when working with WPF. – Luis Filipe Apr 27 '15 at 11:11
  • I'm not sure why everyone keeps repeating this. You can specify a usercontrol (or any other control) as the root element of a XAML file; the resultant class will inherit from the usercontrol. What happens in the XAML of the subclass depends on the content property of the root base control. – Zev Spitz May 02 '23 at 09:09