Click here to Skip to main content
15,888,351 members
Articles / Desktop Programming / WPF
Tip/Trick

Simple Image Button in Silverlight

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
22 Jul 2011CPOL 19.9K   1   2
Simple Image Button

Introduction


In this article we'll see how to create an custom image button by adding an image and textblock to the existing button control to derive our new ImageButton control

Using the Code


Create a new silverlight application. and add an folder called "Themes".
Right click the created "Themes" Folder and Select "New item" and choose resource dictionary and name it as generic.xaml
Set the Build Action of the file to Page - which is the default one.
 ....
<Style TargetType="Custom:MyButton">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="Custom:MyButton">
            <Grid x:Name="Root" Background="AliceBlue">
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width=".30*"/>
                  <ColumnDefinition Width=".70*"/>
               </Grid.ColumnDefinitions>
               <Image 
                  Source="/SilverlightApplication1;component/Images/exclamation1.png" 
                  Width="30"
                  Height="30" Grid.Column="0"></Image>
               <TextBlock Text="My Button" Grid.Column="1"
                  VerticalAlignment="Center"></TextBlock>
            </Grid>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>
.....

Add a new class to the project which extends the Button
In the constructor you have to assign the type of your class to DefaultStylekey. Which is more important otherwise your button won't show up the style declared in generic.xaml.

MyButton.cs:

C#
public class MyButton : Button
    {
        public MyButton()
        {
            this.DefaultStyleKey = typeof(MyButton);
        }
    }

Just add reference to our control and we can use it like any other silverlight control

MainPage.xaml:

...
  <Grid x:Name="LayoutRoot" Background="White">
        <Custom:MyButton  Width="100" Height="30" Click="MyButton_Click">
        </Custom:MyButton>
  </Grid>
...

Points of Interest


We can control the visual state of the control by adding visual state manager to our generic.xaml

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Tikona Digital Networks Pvt. LTD
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 What is this about? Is it about crea... Pin
Jürgen Röhr27-Jul-11 20:15
professionalJürgen Röhr27-Jul-11 20:15 
GeneralReason for my vote of 5 Great way of writing that is of a lo... Pin
Paul McKillop26-Jul-11 19:01
professionalPaul McKillop26-Jul-11 19:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.