-1

我已经动态创建了按钮,现在无法处理它上面的事件,互联网上没有有用的链接..

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)
{

}

此代码不起作用...

4

3 回答 3

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;
}
于 2013-10-21T11:36:59.297 回答
0

尝试 MouseLeftButtonDown 和 MouseLeftButtonUp 事件。在 MouseLeftButtonDown 中将图像更改为新图像,然后使用 MouseLeftButtonUp 将其更改回来。

于 2013-06-25T12:33:08.117 回答
0

MouseEnterMouseLeave分别在鼠标指针悬停在对象上和离开对象时触发。您要搜索的事件是Click

除此之外,您的代码应该可以工作:

button.Click += new RoutedEventHandler(button_Click);

void button_Click(object sender, RoutedEventArgs e)
{
    // Whatever
}
于 2013-06-25T09:19:47.423 回答