简体   繁体   中英

Change Background image of button or Label in WPF/XAML on Mouse Hover

I had a Picture Box in Windows Forms using as a button named pbSignin. I managed to change the background of this picture box on mouse hover and also put the function of signin under this picture box.

Now I want the same to happen in WPF Application, but it gives error in WPF, I have no idea what to do.... Please Help

   pbSignin.MouseEnter += new EventHandler(pbSignin_MouseEnter);
   pbSignin.MouseLeave += new EventHandler(pbSignin_MouseLeave);



   private void pbSignin_MouseLeave(object sender, EventArgs e)
    {
        this.pbSignin.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.RedSignin));
    }

    private void pbSignin_MouseEnter(object sender, EventArgs e)
    {
        this.pbSignin.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.BlueSignin));
    }

Here is an example of how to change the background color of a button on mouse over. Just apply to the style to your control, change the TargetType and Setter properties for your picturebox and you should be good to go.

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Green"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

The first Setter, outside the trigger, is the default value for the property. When the trigger condition fires it will overwrite the default value and revert it once the trigger is no longer firing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM