In Expression Blend for Windows Phone, how can I change the image source of a button in different states. I want different images to be displayed in normal state and pressed state.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A couple of options come to mind, depending on what exactly you’re trying to accomplish.
Once scenario is that you have a button that you want to act like a toggle, where you press it once and it stays pressed until you press it again, like a play button on a tape recorder (remember those?).
If that’s what you’re looking for, I would create a
booleanproperty in your codebehind file, such asIsPlaying, that indicates whether your button is currently pressed or not.Then you can create a
ValueConverterthat translates thetrueandfalsevalues to different images. Here’s a write-up on creating aValueConverter. The article talks about converting aboolto aVisibility, but the concept is the same.In your
BooleanToImageconverter, or whatever you name it, you’ll need to decide which image to show and load it up as aBitmap. Then in your.xamlyou’ll bind to theIsPlayingproperty and set the converter toBooleanToImage. This approach works quite well for a lot of scenarios, and it’s nice and tidy, as you don’t have to think in terms of which image is playing; you can think about whether your appIsPlaying.The other scenario is that you want to display the image briefly when the button is pressed, like an animation, then return to the previous state. In that case, your best bet is using the
VisualStateManager, which allows you to define different appearances for your button in various states, such asNormalandClicked. You can apply animations to transition between the states. Here’s a walk-through that demonstrates using theVisualStateManager.