[WPF] Custom button style

Source: Internet
Author: User

With WPF, everything will become simple ..

StyleCodeMore, please refer to the attachment!

/Files/lovebread/buttonstyle.rar

 

Controltemplate specifies the visual structure and behavior of the control. You can specify a new controltemplate for the control to customize its appearance. After creating a controltemplate, you can change its appearance without changing the functions of an existing control. For exampleProgramThe button in is set to a circle instead of the default square, but the button still triggers the click event.

 

There are three methods to define a template:

1. inline definition:

 <  Button   Content  =  "  Button1  "  > 
< Button. Template >
< Controltemplate Targettype = " Button " >
<! -- Define the controltemplate here. -->
</ Controltemplate >
</ Button. Template >
</ Button >

2. defined as a resource:
 <  Stackpanel  > 
< Stackpanel. Resources >
< Controltemplate Targettype = " Button " X: Key = " Newtemplate " >
<! -- Define the controltemplate here. -->
</ Controltemplate >
</ Stackpanel. Resources >
< Button Template = " {Staticresource newtemplate} " Content = " Button1 " />
</ Stackpanel >

3. Define using style:
<  Stackpanel  > 
< Stackpanel. Resources >
< Style Targettype = " Button " X: Key = " Newtemplate " >
< Setter Property = " Template " >
< Setter. Value >
< Controltemplate Targettype = " Button " >
<! -- Define the controltemplate here. -->
</ Controltemplate >
</ Setter. Value >
</ Setter >
</ Style >
</ Stackpanel. Resources >
< Button Style = " {Staticresource newtemplate} " Content = " Button1 " />
</ Stackpanel >


Use templatebinding to retain control attributes

After creating a new controltemplate, you may still want to use public properties to change the control's appearance. Templatebinding tag Extension
The attributes of the element in controltemplate are bound to the public attributes defined by the control.

Changes the widget appearance Based on the widget status.
You can use the visualstate object to specify the display of a control in a specific State. Visualstate contains storyboard for Modification
The appearance of the elements in controltemplate. This can be achieved without writing any code, because the control logic can be used
Visual statemanager to change the status. Control to enter the visualstate. NameThe dashboard starts to work when the status of the attribute is specified.
When the control exits, storyboard stops.
 

 

 

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.