0

I want to create an image as a button in code in C# WPF (not a button with BG image but an actual image). I read on this site to use a PictureBox for the image, and I've found that the WPF equivalent is Image. The problem is, that while i've found PictureBox has a .Click that you can set, Image does not. The two things I want to do are:

  1. Create an array of buttons that are images and can be clicked.
  2. Have an image for the unclicked and clicked states of the button.

Is there anything right in front of me I'm missing?

Here is my loop creating the buttons:

sideBarButtons = new Button[infoLoader.categoriesLength];
            sideButtons = new Image[infoLoader.categoriesLength];
            ImageBrush[] myBg = new ImageBrush[infoLoader.categoriesLength];
            for (int i = 0; i < sideBarButtons.Length; i++)
            {
                myBg[i] = new ImageBrush();
                myBg[i].ImageSource = new BitmapImage(graphicLoader.buttonUnselected[(i % myBg.Length)]);

                /*sideBarButtons[i] = new Button();
                sideBarButtons[i].Content = infoLoader.categories[i].name;
                sideBarButtons[i].Background = myBg[i];
                //sideBarButtons[i].BorderThickness = ;
                sideBarButtons[i].Width = 155;
                sideBarButtons[i].Height = 46;
                Canvas.SetLeft(sideBarButtons[i], 30);
                Canvas.SetTop(sideBarButtons[i], 10 + (46 * i));
                sideBarButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
                leftSideBar.Children.Add(sideBarButtons[i]);*/

                BitmapImage myBmp = new BitmapImage();
                myBmp.BeginInit();
                myBmp.UriSource = myBg[i];
                myBmp.EndInit();

                sideButtons[i] = new Image();
                sideButtons[i].Source = myBmp;
                sideButtons[i].Width = 155;
                sideButtons[i].Height = 46;
                Canvas.SetLeft(sideButtons[i], 30);
                Canvas.SetTop(sideButtons[i], 10 + (46 * i));
                sideButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
                leftSideBar.Children.Add(sideButtons[i]);

            }

The first commented out area is when I was creating buttons with buttons and not images, while the second is images and it doesn't work. Thanks in advance.

1 Answer 1

2

Two options here:

1.) Instead of using the Click event, which doesn't exist on Image, use MouseDown, which does.

2.) Instead of using Images and repurposing them, use Buttons with a custom style on it. Then you can handle the button's click.

Personally, I'd use the latter, but really either works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.