The translation of programming WPF 9th 5. Default Visualization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.