Reputation: 17045
After custom control is created there automatically appeared file for C# code - MyCustomControl.cs:
public class MyCustomControl : ContentControl {
static MyCustomControl( ) {
...
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl),
new FrameworkPropertyMetadata(typeof(MyCustomControl)));
}
...
}
and file for default stile - Themes\Generic.xaml:
<!-- themes/generic.xaml -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControlLib">
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
But where and how should I correctly place XAML code for layout and content of Custom Control itself?
Upvotes: 1
Views: 1796
Reputation: 74802
The (default) layout and content of the custom control is defined by the ControlTemplate in generic.xaml. So you should put the layout and content within the ControlTemplate element that has been generated for you. (Note that the ContentPresenter will display content provided by users of your control: you need only to provide "content" that is part of your template, e.g. in a check box, your template would provide the little square but user content would provide the caption.)
Upvotes: 3