#340 – Create a Button with an Image
July 12, 2011 Leave a comment
To create a Button that has an image on the surface of the button, rather than text, you use an Image control as the content of the button.
You need to tell the Image control where to find its image. The easiest way to locate images is to just include them as resources in your project.
- Embed the image as a binary resource in your project
- Set the Build Action of the image to Resource
- Use the filename as the image’s Source
In the example below, we’ve added the Misc-Settings-icon.png file to our project.
We can then create a Button that has this image as its main content. Because Button is a ContentControl, it can contain another control as its content.
<StackPanel> <Button HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" > <Image Source="Misc-Settings-icon.png" Height="64" Width="64"/> </Button> <Label Content="That's a button up there.." HorizontalAlignment="Center"/> </StackPanel>