Jump to content



Photo

c# XAML Change button image

Answered Go to the full post

  • Please log in to reply
6 replies to this topic

#1 James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 12 September 2013 - 18:33

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



Best Answer Kalint , 12 September 2013 - 19:19

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...source-on-click

Go to the full post



#2 Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 12 September 2013 - 19:19   Best Answer

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...source-on-click



#3 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 12 September 2013 - 19:37

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...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



#4 Descartes

Descartes

    Neowinian

  • Joined: 29-November 08
  • Location: Poland

Posted 12 September 2013 - 19:58

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.



#5 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 12 September 2013 - 21:52

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.



#6 Descartes

Descartes

    Neowinian

  • Joined: 29-November 08
  • Location: Poland

Posted 12 September 2013 - 23:57

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.



#7 greenwizard88

greenwizard88

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 28-November 04

Posted 16 September 2013 - 22:05

You can also use a converter, so you can simply databind the button to a property.

 

Like this: http://stackoverflow...-to-a-converter