Search code examples
c#buttonbackgroundwindows-phoneblend

Windows Phone c#, how to change button image when in pressed state?


For a C# Windows Phone app, how does one have a button that has a .png background, and then the background changes upon a press? To word it a little differently, how can I change the button's background image for the pressed state?

I have a button that I set the background through blend to be a png image. When I go to blend, right click the button and click edit a copy for the template, I click on the pressed state and it goes red showing it is recording. I then go to change the background image but there is a yellow border around it. I cannot change it. If I reset the value so the border goes away I get a warning saying I broke animations. If I ignore it and set it anyway, it sets the "pressed" image as the default image and all states have that image now.

I can't figure out how to make this button just be an image and then display a different image when pressed, and back to the original image when unpressed.


Solution

  • This is how I do it programatically to create a "blink" effect when someone presses one of my four arrow images from my on-screen control pad (I re-load the same image, you could load your other image instead):

    private void imLeft_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        _wormDirection = Direction.Left;
        ((Image)sender).Source = null;
    }
    
    private void imLeft_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        System.Threading.Thread.Sleep(25);
        ((Image)sender).Source = imLeftImageSource;
    }