我动态创建了按钮,现在无法处理它上面的事件,互联网上没有有用的链接..
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
...
void button_MouseLeave(object sender, EventArgs e)
{
}
void button_MouseEnter(object sender, EventArgs e)
{
}
此代码无效...
答案 0 :(得分:0)
MouseEnter
和MouseLeave
。您要搜索的活动是Click
。
除此之外,您的代码应该有效:
button.Click += new RoutedEventHandler(button_Click);
void button_Click(object sender, RoutedEventArgs e)
{
// Whatever
}
答案 1 :(得分:0)
尝试使用MouseLeftButtonDown和MouseLeftButtonUp事件。在MouseLeftButtonDown中将图像更改为新图像,然后使用MouseLeftButtonUp将其更改回来。
答案 2 :(得分:0)
如此简单,只需按原样放置此代码即可。
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
void button_MouseLeave(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camBlue.png", UriKind.Relative));
Button1.Background = brush;
}
void button_MouseEnter(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camRed.png", UriKind.Relative));
Button1.Background = brush;
}