×
Namespaces

Variants
Actions
(Difference between revisions)

Creating a button with an image and text in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
pavan.pareta (Talk | contribs)
(Pavan.pareta - - Implementation)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Windows Phone]][[Category:Silverlight]][[Category:UI]][[Category:Code Examples]]
+
[[Category:UI on Windows Phone]][[Category:Code Examples]][[Category:Windows Phone 7.5]][[Category:XAML]][[Category:Windows Phone 8]]
{{Abstract|This article explains how to create a Button with an image and text in Windows Phone 7.}}  
+
{{Abstract|This article explains how to create a Button with an image and text in Windows Phone.}}  
 
{{SeeAlso|
 
{{SeeAlso|
 
* [http://msdn.microsoft.com/en-us/library/hh487169(v=vs.92).aspx Button Control Design Guidelines for Windows Phone]
 
* [http://msdn.microsoft.com/en-us/library/hh487169(v=vs.92).aspx Button Control Design Guidelines for Windows Phone]
Line 12: Line 12:
 
|sdk= [http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27570 Windows Phone]
 
|sdk= [http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27570 Windows Phone]
 
|platform= Windows Phone
 
|platform= Windows Phone
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
+
|devicecompatability= All
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
Line 38: Line 38:
  
 
== Implementation ==
 
== Implementation ==
Create a project with any name of your choice ('''ContentControl''' in this example) and place a button with it. As soon as you place the button you will see the ''MainPage.xaml'' will change to code much like the following.
+
Create a project with any name of your choice ('''ContentControl''' in this example) and place a button in the '''mainPage.xaml''' file. As soon as you place the button you will see the '''MainPage.xaml''' will change to code much like the following.
 
<code xml><Button Content="Button" HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center"  /></code>
 
<code xml><Button Content="Button" HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center"  /></code>
  
Line 44: Line 44:
 
<code xml><Button HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center"  /></code>
 
<code xml><Button HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center"  /></code>
  
Now we need to add an image to the project to use for the button:
+
Now we need to add an image to the project so that it can be later used as a button's image:
 
# Right click on project and select '''Add->New Folder'''.  
 
# Right click on project and select '''Add->New Folder'''.  
 
#:A blank folder will be generated. Rename it to "Image".  
 
#:A blank folder will be generated. Rename it to "Image".  
Line 59: Line 59:
 
</code>
 
</code>
  
You can see in the designer that the picture is now part of your button. We add a '''<TextBlock>''' below this image - using either the toolbox (drag'n'drop) or typing the xml code of TextBlock. After adding TextBlock your above code will look something like this.
+
You can see in the designer view that the picture is now part of your button. We add a {{Icode|TextBlock}} below this image - using either the toolbox (drag'n'drop) or typing the xml code of {{Icode|TextBlock}}. After adding {{Icode|TextBlock}} your above code will look something like this.
 
<code xml>
 
<code xml>
 
<Button HorizontalAlignment="Center" VerticalAlignment="Center" >
 
<Button HorizontalAlignment="Center" VerticalAlignment="Center" >

Latest revision as of 07:25, 5 October 2013

This article explains how to create a Button with an image and text in Windows Phone.

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
Device(s): All
Article
Created: girishpadia (17 Oct 2011)
Last edited: pavan.pareta (05 Oct 2013)

[edit] Introduction

Most buttons on Windows Phone are relatively "flat" and are labelled with text. This text is defined in the Content parameter (a ContentControl) of the Button XAML definition:

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

This example shows how you can replace the button text with a picture.

ContentControl.jpg

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

[edit] Implementation

Create a project with any name of your choice (ContentControl in this example) and place a button in the mainPage.xaml file. As soon as you place the button you will see the MainPage.xaml will change to code much like the following.

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

Remove this content property from this XML text (Content="Button") so that your code should be simillar to below.

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

Now we need to add an image to the project so that it can be later used as a button's image:

  1. Right click on project and select Add->New Folder.
    A blank folder will be generated. Rename it to "Image".
  2. Add an image to this new "Image" folder. "You can do this by right click on Image folder and select Add->Existing item

For this example the image name is "myimage.JPG".

Now again go back to your button code in XAML file and add the image as shown:

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

You can see in the designer view that the picture is now part of your button. We add a TextBlock below this image - using either the toolbox (drag'n'drop) or typing the xml code of TextBlock. After adding TextBlock your above code will look something like this.

<Button HorizontalAlignment="Center" VerticalAlignment="Center" >
<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.

[edit] References

See the examples section of ContentControl Class for other examples of setting button content.

This page was last modified on 5 October 2013, at 07:25.
974 page views in the last 30 days.
×