如何在鼠标单击时更改按钮的背景图像,创建“动态”? - Windows Phone

时间:2013-06-25 09:05:36

标签: windows-phone-8 windows-phone

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

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

}

此代码无效...

3 个答案:

答案 0 :(得分:0)

当鼠标指针悬停在对象之上和之外时,分别触发

MouseEnterMouseLeave。您要搜索的活动是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;
}