Winrt: Experience controltemplate-define a simple button control Template

Source: Internet
Author: User

 

It is just a simple button control template. However, color changes are gradient. The foreground, background, and border settings are the same as normal buttons. When the mouse points to a high-brightness color, the color after clicking is false and the color of isenabled can be defined in btnpressed by modifying the XAML resource, btnhighlighted and btndisabled resources. When the value of button. isenabled is false, the background changes to transparent, and the foreground and borderbrush are set to btndisabled resource-defined colors.

 

You can modify the color gradient time by modifying the generatedduration attribute of visualtransition in visualstategroup. transitions.

 

In general, the control template in winrt is similar to wpf4 (there were many differences with wpf4 before, and there was no visualstatemanager at that time) and Silverlight. However, there are also some small details. For example, in wpf4, both visualstategroup and visualstate have the name attribute. In Silverlight and winrt, X: name must be used. In WPF and Silverlight, the mouse pointing to the state name in commonstates of the button is Mouseover, but the name in winrt is pointerover. (Pointer fully covers the word "Mouse" in winrt, because winrt supports desktop and tablet frameworks, so pointer is used to represent any mouse or touch screen operations)

 

XAML code:

<! -- Required color definition -->

<Color x: Key = "btnpressed"> black </color>

<Color x: Key = "btnhighlighted"> gray </color>

<Color x: Key = "btndisabled"> gray </color>

 

<! -- Mybuttonstyle -->

<Style X: Key = "mybuttonstyle" targettype = "button">

<Setter property = "padding" value = "10"/>

<Setter property = "background" value = "Transparent"/>

<Setter property = "template">

<Setter. value>

<Controltemplate targettype = "button">

<Border background = "{templatebinding background }"

Borderbrush = "{templatebinding borderbrush }"

Borderthickness = "{templatebinding borderthickness }"

Name = "border">

<Contentpresenter margin = "{templatebinding padding}" name = "content"/>

 

<Visualstatemanager. visualstategroups>

<Visualstategroup X: Name = "commonstates">

<Visualstate X: Name = "normal"/>

<Visualstate X: Name = "pointerover">

<Storyboard>

<Coloranimation storyboard. targetname = "border" storyboard. targetproperty = "(border. Background). (solidcolorbrush. Color )"

Duration = "0" to = "{staticresource btnhighlighted}"/>

</Storyboard>

</Visualstate>

<Visualstate X: Name = "pressed">

<Storyboard>

<Coloranimation storyboard. targetname = "border" storyboard. targetproperty = "(border. Background). (solidcolorbrush. Color )"

Duration = "0" to = "{staticresource btnpressed}"/>

</Storyboard>

</Visualstate>

<Visualstate X: Name = "disabled">

<Storyboard>

<Coloranimation storyboard. targetname = "border" storyboard. targetproperty = "(border. Background). (solidcolorbrush. Color )"

Duration = "0" to = "Transparent"/>

<Coloranimation storyboard. targetname = "border" storyboard. targetproperty = "(border. borderbrush). (solidcolorbrush. Color )"

Duration = "0" to = "{staticresource btndisabled}"/>

<Coloranimation storyboard. targetname = "content" storyboard. targetproperty = "(contentcontrol. foreground). (solidcolorbrush. Color )"

Duration = "0" to = "{staticresource btndisabled}"/>

</Storyboard>

</Visualstate>

<Visualstategroup. transitions>

<Visualtransition generatedduration = "0: 0. 5"/>

</Visualstategroup. transitions>

</Visualstategroup>

</Visualstatemanager. visualstategroups>

</Border>

</Controltemplate>

</Setter. value>

</Setter>

</Style>

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.