J4N
J4N

Reputation: 20697

WPF: Create a UserControl or a CustomControl?

I'm not very clear of when to use a CustomControl and when to use an UserControl. I basically know what CustomControl allow(more customization when using with template).

I want to make a "File browse" control in WPF(TextBlock that displays the current path + Button that trigger a "Open File Dialog").

I'm not sure, because at some places I find that they say this should always "replace" a WPF control. I was more thinking that a a CustomControl was more like a way to display and edit one new semantic value (in my case, a "File(path)").

So, if we don't take into account which one is easier to do, what would be the more adequate between CustomControl and UserControl for the "FileBrowse" control that I'm speaking about?

Thank you

Upvotes: 3

Views: 2257

Answers (3)

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

Custom Control:

  • A loosely coupled control w.r.t code and UI
  • Derives from Control
  • Defines UI in a ResourceDictionary
  • UI can be changed in different projects
  • Has full toolbox support
  • Defines a single control
  • More flexible

User Control :

  • A tightly coupled control w.r.t code and UI
  • Derives from UserControl
  • Defines UI as normal XAML
  • UI is fixed and can't have different looks in different project
  • Can't be added to the toolbox
  • Defines a set of controls
  • Not very flexible like a Custom Control

When we talk about differences, it is more important to emphasis on the context when to use what:

  • When you have a rapid and fixed content in your UI, use UserControl.
  • When you want to separate some basic functionality of your main view to some smaller pieces with reusability, use UserControl.
  • When you want to use your control in different projects and each project may want to change the look, use CustomControl.
  • When you want to implement some additional functionality for a control, create a CustomControl derived from the base control.
  • When you want to apply themes to your controls, use CustomControl.
  • When you want to add toolbox support for your control, so that your user will be able to do drag and drop to the designer, use CustomControl.

Upvotes: 6

blueprint
blueprint

Reputation: 308

I think a UserControl is the one to choose, for it is used for a kind of "assembly of existing controls". In your case a button and a file open dialog. It will then get a specific look and feel (for example the default look of a button and the default look of a file open dialog).

The CustomControl is kind of the other way round. It has no look and feel by itself. It is completely abstract concerning layout. The layout comes into play, wwhen assigning a style to it.

Upvotes: 1

Mark Feldman
Mark Feldman

Reputation: 16119

In general a custom control extends an existing control while a user control creates a new control type from a collection of existing controls. I would say that a user control is better suited, based on the information you've given.

Upvotes: 0

Related Questions