简体   繁体   中英

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 aa 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

Custom Control:

  • A loosely coupled control wrt 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 wrt 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM