简体   繁体   English

如何在后面的代码中将图像绑定到Button?

[英]How to bind an image to a Button in code behind?

I have a default WPF button and a image, separated distance apart. 我有一个默认的WPF按钮和一张图像,彼此分开。 When I hover or press the button, the button gets the standard blue or grey effect. 当我将鼠标悬停或按下按钮时,该按钮将获得标准的蓝色或灰色效果。 What I want to achieve is, when I hover or press the image, I would also get the same hover and press effect on the button as if I'm hovering or pressing the button itself. 我想要实现的是,当我悬停或按下图像时,我也将获得相同的悬停并在按钮上按下效果,就像我悬停或按下按钮本身一样。

How to bind this is code behind? 如何绑定这是背后的代码? Can someone provide me a simple code snippet? 有人可以给我提供一个简单的代码段吗?

Sub Image_MouseEnter() Handles YourImage.MouseEnter
    YourButton.CaptureMouse()
End Sub

Sub Image_MouseLeave() Handles YourImage.MouseLeave
    YourButton.ReleaseMouseCapture()
End Sub

Hope this helps. 希望这可以帮助。

Why don't you use css instead of code behind? 为什么不使用CSS而不是后面的代码?

.addButton { background: url("../images/add_buttons_b-02.png") no-repeat 0 0 transparent; color: #FFFFFF; font-size: 0px; width: 100px; height: 28px; border: none; cursor: pointer; }
.addButton:hover { background: url("../images/add_buttons-02.png"); }

Just simply add css for the button :"> 只需为按钮添加CSS即可:“>

Hope this helps 希望这可以帮助

Btw, you are the one who ask the same question several minutes ago, right? 顺便说一句,您是几分钟前问相同问题的人,对吗? :"> :“>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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