-1

Trying to either change the background image of a button when it's clicked or change the button into an image when clicked.

 var brush = new ImageBrush
            {
                ImageSource = new BitmapImage(new Uri("image1.jpg", UriKind.Relative))
            };
            Button1.Background = brush;
Usman Afzal
  • 376
  • 3
  • 9
  • 1
    Does this answer your question? [Programmatically changing button icon in WPF](https://stackoverflow.com/questions/5971300/programmatically-changing-button-icon-in-wpf) – Usman Afzal Jan 31 '20 at 23:33
  • WPF/UWP and XAML are deigned with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's power and runs into problem at every other corner. I wrote a introduction into MVVM a few yeras back: https://social.msdn.microsoft.com/Forums/vstudio/en-US/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2/lets-talk-about-mvvm?forum=wpf | This Image like rerepsents one of two things. 1) A Bool being set 2) A bunch of settings you itterate through. In thise case it is a mater of basic binding, plus maybe a converter. – Christopher Feb 01 '20 at 00:23

1 Answers1

-1

If you are using UWP or WPF, you can do it in this way (which is exactly what you already have):

ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("uri to image"));
btn.Background = brush1;

or as described here: example

Clemens
  • 123,504
  • 12
  • 155
  • 268
Guillermo Gerard
  • 808
  • 10
  • 21