Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

One Step to Create an Image Button for Silverlight Applications

0.00/5 (No votes)
20 Apr 2010 1  
I had to create a trivial image button for my Silverlight web site. I Googled the web and got tons of tips and suggestions. However, most of them involve several editing steps through resources, template or style. Some require additional tools, such as Expression Blend. In my...
I had to create a trivial image button for my Silverlight web site. I Googled the web and got tons of tips and suggestions. However, most of them involve several editing steps through resources, template or style. Some require additional tools, such as Expression Blend.

In my implementation, I was able to create an image button by adding one line to the XAML code. What I post here isn't really a trick, but a tip about a simple way of doing simple things.

Here is the starting-point XAML code for my minimal Silverlight page that contains a button in the centre:

XML
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
    <Button x:Name="MyButton" Height="32" Width="32">
    </Button>
  </Grid>
</UserControl>


If you add one line of code as follows, you will overlay "someimage.png" on the button immediately:

XML
<Button x:Name="MyButton" Height="32" Width="32">
      <Image Source="someimage.png" Height="32" Width="32"></Image>
</Button>


In fact, the code above is equivalent to:

XML
<Button x:Name="MyButton" Height="32" Width="32">
      <Button.Content>
            <Image Source="someimage.png" Height="32" Width="32"></Image>
      </Button.Content>
</Button>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here