Namespaces

Variants
Actions

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 over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

Creating a button with an image and text in Windows Phone

From Wiki
Jump to: navigation, search
girishpadia (Talk | contribs)
(Girishpadia -)
girishpadia (Talk | contribs)
(Girishpadia -)
Line 1: Line 1:
[[Category:Draft]][[Category:Windows Phone]][[Category:Silverlight]]
+
[[Category:Windows Phone]][[Category:Silverlight]]
 
{{Abstract|This article explains how to define ContentControl in WP}}  
 
{{Abstract|This article explains how to define ContentControl in WP}}  
  

Revision as of 17:37, 17 October 2011

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
Article
Created: girishpadia (17 Oct 2011)
Last edited: girishpadia (17 Oct 2011)


Contents

Introduction

ContentControl is a class which provides the button’s Content property. Button derives from Control but it cab be also derives 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.

Description

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" Margin="6,100,6,101" Width="444">
<StackPanel>
<Image Source="Image\myimage.JPG" Stretch="Fill" Height="334" Width="339" />
</StackPanel>
</Button>

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" Margin="6,100,6,101" Width="444">
<StackPanel>
<Image Source="Image\myimage.JPG" Stretch="Fill" Height="334" Width="339" />
<TextBlock Text="This button has picture!" TextAlignment="Center" />
</StackPanel>
</Button>


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.

Image

ContentControl.jpg

Note

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

1225 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×