Reputation: 23
I have a simple question. I want to add an icon to a C# WPF Button control. I do not want to have to write C# code, or edit XAML to do this.
However, when I click on the button in the Designer, there is no option under properties to set an image. How do you do this through the Visual Studio GUI?
Upvotes: 2
Views: 24073
Reputation: 11
you could install FontAwesome
on your Computer
Goto http://fontawesome.io
and download. Open the downloaded ZIP file and inside the font folder there should be .otf file, Install that!
and in the content of the button you could simply type the desired icons code!
For eg: if you want your button to look like this
then install FontAwesome Font
and in your button tag type Content="" FontFamily="FontAwesome"
More codes can be found here http://fontawesome.io/cheatsheet/
Upvotes: -1
Reputation: 21
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. A text shows that asks you to press ALT to replace the text of the button with the image.
I currently don't know how to get both, image and text, for a button.
in XAML, it looks like this:
<Button x:Name="button12" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75">
<Image x:Name="image" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="24" Source="gfx/new.png"/>
</Button>
Upvotes: 2
Reputation: 61379
The easiest/best way to do this is to add an Image
control as the Content
of the Button
.
The property window is somewhat limited in what it can do, and only supports text for that property. This does include bindings, so you could use an Image
StaticResource
. I couldn't find an easy way to create one from the property designer either though.
So basically, you are stuck with editing XAML. Either with a direct Content
property or by creating an element in Resources
Its not that bad! Just write:
<Button>
<Button.Content>
<Image ImageSource="..."/>
</Button.Content>
</Button>
Now of course, you could create a custom button that exposed that property via the designer, but thats even more XAML. Its WPF, you are going to have to write XAML, so learning how should be a priority.
Upvotes: 6