Original: "Programming WPF" translation 9th 5. Default Visualization
While the ability to provide a custom appearance for a control is useful, developers should be able to use a control without having to provide custom visualizations. This control should work exactly when it is used in its most direct way. This means that the control should provide a default set of values.
These default visualizations are stored in the component's binary resource, and the source file used is theme "Generic.xaml. If you create a project for a WPF control library in Visual Studio 2005 , this will automatically add this file to your project and set its Build Action to be embedded as a resource. (See Chapter 6 For more information on how to compile XAML resources in a component )
in the theme "Generic.xaml file, define a style that carries the TargetType to specify your control. This style should set the Template property with a ControlTemplate tag and define a default visualization for your control, as shown in example 9-17. See Chapter 5 For more information on how to define a style that provides a template.
Example 9-17
<?Mapping xmlnamespace= "Local" clrnamespace= "Customcontrollib"?>
<ResourceDictionary
xmlns= "http://schemas.microsoft.com/winfx/avalon/2005"
Xmlns:x= "http://schemas.microsoft.com/winfx/xaml/2005"
Xmlns:local= "Local"
>
<StyleTargetType="{x:type Local:mycustomcontrol}">
<Setter Property= "Template">
<Setter.value>
<ControlTemplateTargetType="{x:type Local:mycustomcontrol}">
<BorderBackground="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.value>
</Setter>
</Style>
</ResourceDictionary>
To make sure that your control gets the default body, you need to let the dependency property system know where the style is. If you don't, you can only get the default value for the base class you choose. Example 9-18 shows how to do this.
Example 9-18
Public classMycustomcontrol:contentcontrol{
StaticMyCustomControl (){
Themestylekeyproperty.overridemetadata (typeof(MyCustomControl),
New frameworkpropertymetadata (typeof(MyCustomControl));
}
}
Notice that Visual Studio 2005 automatically generates this code for you when you add a new custom control to the project in your control library.
The translation of programming WPF 9th 5. Default Visualization