Sign in to follow this  
Followers 0
James Rose

c# XAML Change button image

7 posts in this topic

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.