Issue where the background picture of a button disappears when you move the mouse over the button in WPF

Source: Internet
Author: User

If you set a background image to the button, when the mouse moves over the button, the button becomes a light blue button, the background image disappears, there are many solutions to this problem, I only share my solution.

The first way I used it was to add a picture to the button without setting it in the background.

<button  horizontalalignment= "left" margin= "179,56.506,0,0" click= "Button_Click" verticalalignment= "Top" Width= "height=" "borderbrush=" #FF1344EC "grid.row=" 1 "padding=" 2 ">            <image source=" Image/dzsp.png " Name= "Btn1image" stretch= "Fill" ></Image>        </Button>

The button set in this way does not appear again, but it also exposes another problem, that is, the button does not react when the mouse moves over the button. I hope that the color of the button will change when the mouse is moved to the button. So I used the following method again.

<button width= "margin=" 0,0,0,4 "name=" my "mouseenter=" My_mouseenter "mouseleave=" My_mouseleave "BorderBrush= "{x:null}" > <Button.Template> <controltemplate targettype= "button" &G                            T <ContentControl> <border borderbrush= "{TemplateBinding BorderBrush}" Borderthickne                                        ss= "{TemplateBinding borderthickness}" > <StackPanel>                                                <stackpanel height= "> <StackPanel.Background>                                            <imagebrush imagesource= "Image/ico_01.png" stretch= "None"/>                                    </StackPanel.Background> </StackPanel> <label borderthickness= "0" padding= "5" fontsize= "+ fontweight=" Semibold "foreground=" WhiTE "horizontalcontentalignment=" Center >XXXX</Label> </stackpanel></borde                    R> </ContentControl> </ControlTemplate> </Button.Template> </Button>

In this way, the original used to do the background of the picture to separate, originally the picture is an icon plus a paragraph of text, now is the icon is separately made as a picture ico_01.png, and then the original background image of the text placed in the label.

Defines the button's two events MouseEnter and MouseLeave, respectively, for handling mouse move to button and move out button

Private voidMy_mouseenter (Objectsender, MouseEventArgs e) {My. BorderBrush=brushes.red; My. BorderThickness=NewThickness (2.0); My. Opacity=0.5; SS++; }        Private voidMy_mouseleave (Objectsender, MouseEventArgs e) {My. BorderBrush=NULL; My. BorderThickness=NewThickness (0.0); My. Opacity=1; }

In this case, run the program, when the mouse enters the button, the button will become translucent, and a red border appears

Issue where the background picture of a button disappears when you move the mouse over the button in WPF

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.