According to all of the documentation, when you're creating a non-lookless control, you're supposed to subclass UserControl
. However, UserControl
is a simple subclass of ContentControl
but it doesn't appear to add anything to it, interface-wise. As such, you can take that designer-generated code and change the base class to ContentControl
and it appears to still work exactly the same.
So what's the point of UserControl
over ContentControl
?
For those who keep answering Visual Studio treats them differently, I'd argue that isn't the case. Try it! Create a new UserControl
in Visual Studio, then in the resulting XAML file, change the root tag to ContentControl
. Then in the associated class file, change the base class to ContentControl
or simply delete it as I have done here (see the note) and you'll see it appears to work exactly the same, including full WYSIWYG designer support.
Note: You can delete the base class from the code-behind because it's actually a partial class with the other 'part' of the class being created by the XAML designer via code-generation. As such, the base class will always be defined as the root element of the XAML file, so you can simply omit it in the code-behind as it's redundant.
Here's the updated XAML...
<ContentControl x:Class="Playground.ComboTest.InlineTextEditor" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <TextBlock Text="Success" /> </ContentControl>
...and the associated class file...
namespace Playground.ComboTest { public partial class InlineTextEditor { public InlineTextEditor() => InitializeComponent(); } }
ContentControl is a base class for controls that contain other elements and have a Content -property (for example, Button ). ContentPresenter is used inside control templates to display content.
Content Control is a base class that provides standardised functionality to WPF Controls. The Content Control class represents controls that can include a single item of content. This content is commonly plain text or a child control. Content Control is a subclass of the Control class in WPF.
A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.
The ControlTemplate allows you to specify the visual structure of a control. The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.
UserControls are a good fit for aggregating existing controls when you don't need to provide the consumer a ControlTemplate. This means that UserControls are not lookless. Why not just use ContentControl as it can have coupled XAML like a UserControl and the implementation looks similar to UserControl? Well, there are several important technical differences you must know:
VisualStateManager.GoToState()
. ContentControl requires the VisualStateGroups to be at the top-level and you must call them with VisualStateManager.GoToElementState()
.ContentControl's ControlTemplate
<ControlTemplate TargetType="ContentControl"> <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" /> </ControlTemplate>
UserControl's ControlTemplate
<ControlTemplate TargetType="UserControl"> <Border BorderBrush="{TemplateBinding Border.BorderBrush}" BorderThickness="{TemplateBinding Border.BorderThickness}" Background="{TemplateBinding Panel.Background}" Padding="{TemplateBinding Control.Padding}" SnapToDevicePixels="True"> <ContentPresenter HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Content="{TemplateBinding ContentControl.Content}" /> </Border> </ControlTemplate>
Basically, the UserControl
class is there for convenience. It enables us to build little parts of the UI from already existing controls, whereas ContentControl
s are really for creating new controls, generally with a single purpose and/or functionality.
I read a book that had a good explanation of this and by good luck, someone has 'put a copy of it online'. From the linked book:
The UserControl class is a container class that acts as a “black box” container for a collection of related controls. If you need a set of three controls to always appear together and be allowed to easily talk to each other, then a likely candidate for making that happen is the UserControl class.
Then relating to whether to create a CustomControl
:
The following is a summary of the decision process:
Use the framework as much as possible. WPF provides a variety of extensible controls, so make sure that the functionality you want doesn’t already exist in a WPF control.
In many cases, the data structure you’re working with requires different visual representation. Using ControlTemplates and DataTemplates can often get you the functionality you need.
Look at ValueConverters to see whether they can help bridge the gap between the stock functionality and what you need.
Finally, see whether you can’t extend existing behavior with attached properties.
Take a look for an in depth answer to your question:
WPF Control Development Unleashed
UPDATE >>>
@MarqueIV, to answer your question more directly: The UserControl
class is provided to us for convenience. That's it. If you add a WPF CustomControl
into your project, you will see that it has no XAML file. This means that you have to design you control markup in a file called Generic.xaml
in the Themes
folder. The UserControl
class gives us a XAML file so that it is easier to create them... so it is more convenient... that's it. That's the reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With