• 0

c# XAML Change button image


Question

Hello gang,

 

I have this button that needs to be changed from Play to Pause but I'm not getting there

<Button Height="40" Grid.Column="0" Background="DarkBlue" x:Name="btnPlayPause" Click="PlayPauseMedia" IsEnabled="False" >
  <Image Source="images\play.png"></Image>
</Button>

I don't care if the answer is in XAML or C#.

 

Thanks kindly

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

In your XAML, the image should also have a name and then in your code behind you can do an if statement to change the image.

 

Something similar to this: http://stackoverflow.com/questions/11244638/change-image-source-on-click

 

That worked perfectly... and also made me feel stupid.  Duh, the Image is an object all on it's own.   <sigh>   Thank you very much for the info

Link to comment
Share on other sites

  • 0

Do you have a public property in your program that shows the current playback state?

 

If so, you could bind the button's Image to that property (and use a converter if necessary).

 

Here is an intro to value converters.

 

Of course the approach proposed by Kalint is also completely valid.

Link to comment
Share on other sites

  • 0

Do you have a public property in your program that shows the current playback state?

 

I was attempting this, but it wasn't working.   But the variable was private so I may have to look again.  Thanks for the info, I'll take another look.   I tend to want to write c# as opposed to XAML.  That may sound derogatory, it's not.  I find the XAML pattern a little more difficult.

Link to comment
Share on other sites

  • 0

I was attempting this, but it wasn't working.   But the variable was private so I may have to look again.  Thanks for the info, I'll take another look.   I tend to want to write c# as opposed to XAML.  That may sound derogatory, it's not.  I find the XAML pattern a little more difficult.

 

To expand on that, the member you'll be trying to bind to must be a property with a public get accessor.

 

Meaning that instead of

public Boolean IsPlaying;

it'll have to be

public Boolean IsPlaying { get; protected set; } //change the setter access modifier according to your needs

And don't worry about finding C# more comfortable to work with than XAML. As I said, there are multiple ways of solving a problem, this is just one of them.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.