WPF Custom button Style (button length adaptive with content length)

Source: Internet
Author: User

The code is as follows:

1  <Stylex:key= "ButtonStyle"TargetType= "button">2         <Setter Property= "Template">3             <Setter.value>4                 <ControlTemplateTargetType= "button">5                     <!--StackPanel is used to control the adaptation of the position when the button length changes. -6                     <StackPanelx:name= "Sppanel"Orientation= "Horizontal"HorizontalAlignment="{TemplateBinding HorizontalAlignment}" >7                         <Grid>8                             <Grid.background>9                                 <ImageBrushStretch= "Fill"ImageSource= "Btn-n.png"/>Ten                             </Grid.background> One                             <grid.columndefinitions> A                                 <ColumnDefinition></ColumnDefinition> -                                 <ColumnDefinition></ColumnDefinition> -                             </grid.columndefinitions> the                             <Borderx:name= "Logoimg"Width= "$"Height= "Wuyi"> -                                 <Border.background> -                                     <ImageBrushStretch= "None"ImageSource= "Btn-icon-up.png"/> -                                 </Border.background> +                             </Border> -                             <!--Viewbox is control when text is longer than the maximum limit, the text is reduced - +                             <ViewboxGrid.column= "1"MaxWidth= " the"> A                                 <Labelx:name= "Lblcontent"Padding= "0,0,5,0"verticalcontentalignment= "Center"Content="{TemplateBinding Content}"/> at                             </Viewbox> -                         </Grid> -                     </StackPanel> -                 </ControlTemplate> -             </Setter.value> -         </Setter> in     </Style>
View Code

Effects such as:

When content becomes longer:

Knowledge to use:

1. StackPanel: Used to control the position of the button, you can set the center, or left and right alignment;

2. Viewbox: When used to achieve long content, reduce the text

* * Code that has been streamlined and added to the trigger:

1  <Stylex:key= "ButtonStyle"TargetType= "button">2         <Setter Property= "Template">3             <Setter.value>4                 <ControlTemplateTargetType= "button">5                     <!--StackPanel is used to control the adaptation of the position when the button length changes. -6                     <StackPanelx:name= "Sppanel"Orientation= "Horizontal"HorizontalAlignment="{TemplateBinding HorizontalAlignment}" >7                         <Stackpanel.background>8                             <ImageBrushStretch= "Fill"ImageSource= "Btn-n.png"/>9                         </Stackpanel.background>Ten                         <Borderx:name= "Logoimg"Width= "+"Height= "+"Margin= "5,0,0,0"> One                             <Border.background> A                                 <ImageBrushStretch= "UniformToFill"ImageSource= "Btn-icon-up.png"/> -                             </Border.background> -                         </Border> the                         <!--Viewbox is control when text is longer than the maximum limit, the text is reduced - -                         <ViewboxGrid.column= "1"MaxWidth= " the"Margin= "5,0"> -                             <Labelx:name= "Lblcontent"verticalcontentalignment= "Center"Content="{TemplateBinding Content}"/> -                         </Viewbox> +                         <!--</Grid> - -                     </StackPanel> +                     <controltemplate.triggers> A                         <Trigger Property= "IsMouseOver"Value= "true"> at                             <SetterTargetName= "Sppanel" Property= "Background"> -                                 <Setter.value> -                                     <ImageBrushStretch= "Fill"ImageSource= "Btn-h.png"/> -                                 </Setter.value> -                             </Setter> -                             <SetterTargetName= "Logoimg" Property= "Background"> in                                 <Setter.value> -                                     <ImageBrushStretch= "UniformToFill"ImageSource= "Btn-icon-right.png"/> to                                 </Setter.value> +                             </Setter> -                         </Trigger> the                     </controltemplate.triggers> *                 </ControlTemplate> $             </Setter.value>Panax Notoginseng         </Setter> -     </Style>
View Code

WPF Custom button Style (button length adaptive with content length)

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.