In winforms there were 2 templates: UserControl
allows to make composite control (group of existing controls) to reuse it and standardize operations with it, CustomControl
was a fully custom control (often rendered manually, performing differently than standard controls, etc), allowing overriding and provides access to protected
things.
In wpf I only found UserControl
template (using express edition of VS).
To create custom MyControl
I am doing following:
- create normal class, based on
Control
/ContentControl
; - create resource dictionary with
Style
containing control template (could be onlyControlTemplate
, style is better, because can contain other properties setter)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MySolution">
<Style x:Key="MyControl" TargetType="local:MyControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyControl">
<Grid x:Name="PART_Grid">
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
- bind them together like this
public class MyControl : Control
{
public MyControl()
{
var dictionary = new ResourceDictionary();
dictionary.Source = new Uri("/MySolution;component/MyControl.xaml", UriKind.Relative);
Style = dictionary["MyControl"] as Style;
Loaded += MyControl_Loaded;
}
}
- define lazy parts accessors in template
private Grid _partGrid;
private Grid PartGrid
{
get
{
if (_partGrid == null)
_partGrid = (Grid)Template.FindName("PART_Grid", this);
return _partGrid;
}
}
It works, but I am not sure if its the most optimal way:
.cs
and.xaml
are separate files (not one entity as in case of wpfUserControl
);- when moving
xaml
(to example, insideControls
folder) constructor code has to be adjusted; - parts accessor are not available in constructor (template is yet not loaded).
My question: is there better way to make custom control with template? More comfortable, automatic, with automatic parts accessors, etc.
Here are my wpf templates
Templated Control
, but I don't have it. There is no online template with such name either. Does my question become "how to get templated control for express edition"? Does it solves issues I listed? Also, you say "control library", I've only "class library" (as a new project) does it matter? – Sinatr