Control Template Learning Notes (ii)

Source: Internet
Author: User
Tags border color

1. Template binding TemplateBinding

When do I use template bindings?

--When you still might want to use public properties to change the appearance of a control, template binding is your perfect choice

How do I use template bindings?

--We also use the button in the learning note, for example, to customize Border's background= "Red" in the template, using TemplateBinding for background= "{TemplateBinding backbround }”;

The background to the left of the equals sign is the background color of border, and the background to the right of the Equals button is the property.

What problems can be solved by using template bindings?

--When multiple buttons render the same visual structure, except for background color or border color, if you don't use TemplateBinding, you might want to define a control template for each button that differs

Maybe just a property. So in order to solve this situation we can use TemplateBinding, examples are as follows:

<ControlTemplatex:key= "Buttontemplate"TargetType= "{x:type button}">            <BorderName= "Border"BorderBrush= "Orange"borderthickness= "3"Cornerradius= "2"Background= "{TemplateBinding Background}"Textblock.foreground= "White"Snapstodevicepixels= "True">                <Grid>                    <RectangleName= "Focuscue"Visibility= "Hidden"Stroke= "Black"strokethickness= "1"Strokedasharray= "1 2"Snapstodevicepixels= "True"></Rectangle>                    <ContentPresenterRecognizesaccesskey= "True"Margin= "{TemplateBinding Padding}"></ContentPresenter>                </Grid>            </Border>            <controltemplate.triggers>                <Trigger Property= "IsMouseOver"Value= "true">                    <SetterTargetName= "Border" Property= "Background"Value= "Darkred" />                </Trigger>                <Trigger Property= "IsPressed"Value= "true">                    <SetterTargetName= "Border" Property= "Background"Value= "Indianred" />                    <SetterTargetName= "Border" Property= "BorderBrush"Value= "Darkkhaki" />                </Trigger>                <Trigger Property= "iskeyboardfocused"Value= "True">                    <SetterTargetName= "Focuscue" Property= "Visibility"Value= "Visible" />                </Trigger>            </controltemplate.triggers></ControlTemplate>
Control Templates
<ButtonContent= "Buttona"HorizontalAlignment= "Left"Margin= "10,10,0,0"VerticalAlignment= "Top"Width= " the"Background= "Red"Template= "{StaticResource buttontemplate}" /><ButtonContent= "Buttonb"HorizontalAlignment= "Left"Margin= "10,47,0,0"VerticalAlignment= "Top"Width= " the"Background= "Green"Template= "{StaticResource buttontemplate}" />

Effect

2. Automatically apply templates

Auto Apply template is not set template= "{StaticResource buttontemplate}" when the button in the project automatically applies the defined control template.

How do I implement an auto-apply template?
--The trick is to use type styles, which automatically affect the appropriate element type and set the template property, as shown in the following example:

<TargetType= "{x:type button}">            <   Property= "Control.template"  Value= "{StaticResource buttontemplate}"/ ></Style>

Note: Although defined in the resources, the x:key attribute is not defined. One of the buttontemplate is the control template defined in the Learning note one

<ButtonContent= "Buttona"HorizontalAlignment= "Left"Margin= "10,10,0,0"VerticalAlignment= "Top"Width= " the" /><ButtonContent= "Buttonb"HorizontalAlignment= "Left"Margin= "10,75,0,0"VerticalAlignment= "Top"Width= " the"  />

effect, in fact, from the Toolbox to add a New button will automatically apply the control template, but now I do not make and upload gif type of file, no way to paste.

Control Template Learning Notes (ii)

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.