Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an Image inside a Button programmatically

In WPF:

<Button Width="24" Height="24" >
    <Image Source="pack://application:,,,/res/x.png" VerticalAlignment="Center"/>
</Button>

How can I mimic this in C#? I can't find any method in the Button class that adds children.

like image 792
Wilson Avatar asked Mar 26 '13 00:03

Wilson


People also ask

How to set image on button?

Use the <input type="image"> Tag to Embed an Image in the HTML Button.

How to add image in button android Studio?

Save the XML file in your project res/drawable/ folder and then reference it as a drawable for the source of your ImageButton (in the android:src attribute). Android will automatically change the image based on the state of the button and the corresponding images defined in the XML.

How do I add an image to a button in Visual Studio?

Visual Studio 2015: create a button. create an image. set the source of the image to the right file of your resources (e.g. a . png file that you included to your project) drag the image over the button.


1 Answers

Button is a Content control so you just have to use the Buttons Content property

Example:

Button myButton = new Button
{
    Width = 24,
    Height = 24,
    Content = new Image
    {
        Source = new BitmapImage(new Uri("image source")),
        VerticalAlignment = VerticalAlignment.Center
    }
};
like image 114
sa_ddam213 Avatar answered Sep 23 '22 14:09

sa_ddam213