Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 00:30, 24 April 2012 by hamishwillee (Talk | contribs)

Creating a button with an image and text in Windows Phone

From Wiki
Jump to: navigation, search

This article explains how to define ContentControl in WP

WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested withCompatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Created: girishpadia (17 Oct 2011)
Last edited: hamishwillee (24 Apr 2012)



ContentControl is a class which provides the button’s Content property. Button derives from Control but it can be also derived from ContentControl. This article explains the concept of ContentControl in Windows Phone. The motive of this article is to create a button with a picture and a text within it. Let's see how to cross this road.


Create a project with any name of your choice (ContentControl in my case) and place a button with it. As soon as you place the button you will see the MainPage.xaml will change with the some code which may look like the following.

<Button Content="Button" HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center"  />

In above code, the button property "Content" is set to "Button". Now remove this content property from this XML text so that your code should be simillar to below.

<Button HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center"  />

Remember that our motive is to create button with an image and a text within it. Now right click on project and select Add->New Folder. A blank folder will be generated. Rename it to "Image". The next step is to search a beautiful picture and add it to the Image folder which we create just now. You can do this by right click on Image folder and select Add->Existing item.. and select your favourite image. In this case the image name is "myimage.JPG". Now again go back to your button code in xaml file. We have already removed the content property from the code. Change the code to following to add the image which we just now added to our project.

<Button HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image Source="Image\myimage.JPG" Stretch="Fill" Height="334" Width="339" />

You can see in the designer that the picture is now part of your button. Stil we need to add text below the image. We can use <TextBlock> to add the text block. You can add the TextBlock from toolbox or you can type the xml code of TextBlock. After adding TextBlock your above code will look something like this.

<Button HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image Source="Image\myimage.JPG" Stretch="Fill" Height="334" Width="339" />
<TextBlock Text="This button has picture!" TextAlignment="Center" />

That's it. We have added a picture and text within the button. Press F5 and run the project and see the output in emulator.




PS: The wonderful flower you are seeing in the above image is captured by my digital camera. :-)

908 page views in the last 30 days.