I was looking some tutorials on how to create custom controls in WinRT, and I have a question.
Let's say I want to create a simple control that contains some stuff, like a Grid with an image on the left and a couple of TextBlocks on the right.
I mean, something simple like:
<Grid Height="100"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.3*"/> <ColumnDefinition Width="0.7*"/> </Grid.ColumnDefinitions> <Image Source"/Assets/someRandomImage.png"/> <StackPanel Grid.Column="1" VerticalAlignment="Center"> <TextBlock Text="Some text" Margin="10,0,10,0" FontSize="24" FontWeight="SemiLight" TextTrimming="CharacterEllipsis"/> <TextBlock Text="Some random description..." Margin="10,5,10,0" FontSize="18" FontWeight="Light" Foreground="Gray" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/> </StackPanel> </Grid>
I would create a UserControl with this content, so I'd be able to see it in the XAML Designer while I'm working on its UI, and I'd add all the Properties and DependencyProperties in the UserControl code behind.
Then I saw that another approach would be to use a Template control, so I'd have to create a class that inherits from the Control class, then use the above XAML code as a template and apply it to the custom control and add all the rest of the logic there.
Of course, I'd also have to add the x:Name property to some those UIElements inside the control to be able to interact with them, but you get the idea.
I was wondering, is it ok to use either one of these two methods, or is better to use one in particular, and why? Also, I like using UserControls since I can see them in the Designer window, and instead I wouldn't be able to do that with a Template, I'd have to run the app and create an instance of the control to see what it actually looks like.
Thanks for your help, I think I'm not the only one with this doubt, so I hope this question will help others as well :D
Sergio
Define a server control class that implements the INamingContainer interface as a container in which to create an instance of the template. This is called the template's naming container.
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.
User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.
The ControlTemplate contains the tree of elements that define the desired look. After you define a ControlTemplate you can attach it to any Control or Page by setting it's TemplateProperty. In this example I am also showing you how to use Triggers with ControlTemplate.
TLDR
A custom (templated) control allows an app to use the Template property to replace the control’s internal element tree. If you don’t need/want your control to have that re-templating feature, then use a UserControl as it’s easier.
UserControl
UserControl
is a lot easier to create with Visual Studio or Blend giving you a decent design view support.Such view is often backed with a corresponding view model if you choose to adopt the MVVM pattern.
One problem with a UserControl
is that while you can reuse it in multiple places in your app - it is difficult to make slight adjustments to the way it looks or behave in different places in your app since it doesn't use templates and the UI tree is loaded in the constructor.
Custom control
custom control
or in some cases templated control
is best suited for a small chunk of UI that serves a single purpose - it visualizes a single, specific type of information.Button
, ToggleButton
, ContentControl
, Slider
, TextBox
or ListView
to add to or override its logic. There are cases though when it makes sense to make one from scratch, subclassing "virtually abstract" Control
, ItemsControl
, RangeBase
, Shape
or even FrameworkElement
(the last two are not templated).Collapsed
to Visible
which allows to defer loading parts of your UI to get performance improvements.Custom panel
A custom panel
is yet another type of UI element that allows to customize how it lays out its children.
Creating a user control is a lot simpler than creating a custom control. For starters a user control has designer support. The disadvantage of a user control is that it is limited in comparison with a custom control.
A user control is excellent if you want to create a control that is a composition of some other controls like in your example but suppose you want to create a special kind of panel, then you really have to create a custom control.
So in summary user control for 'simple' controls user control for complex controls. Any user control can be created by a custom control but not the other way around.
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