Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 628 |  Welcome, Guest!   Register  Login
Home > Articles > Silverlight > Silverlight Controls Part -2

Silverlight Controls Part -2

Article posted by Sheonarayan on 12/8/2011 | Views: 1316 | Category: Silverlight | Level: Beginner | Points: 250 red flag


In this Article we are going to learn Silverlight controls like Grid and TextBlock and related properties.

This article is the continuation of my last article in Silverlight controls series, read last article here.

Lets start this article by learning Grid Silverlight control.

Grid

Grid is the most powerful control in Silverlight; it allows us to define rows and columns to place child elements into it.

Get 500+ ASP.NET web development Tips & Tricks and ASP.NET Online training here.

Code

<Grid x:Name="LayoutRoot" Background="White">

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition />

<RowDefinition />

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition />

<ColumnDefinition />

</Grid.ColumnDefinitions>

<TextBlock Grid.Column="0" Grid.Row="0" Text="First row first cell"/>

<TextBlock Grid.Column="1" Grid.Row="0" Text="First row second cell"/>

<TextBlock Grid.Column="0" Grid.Row="1" Text="Second row first cell"/>

<TextBlock Grid.Column="1" Grid.Row="1" Text="Second row second cell"/>

<TextBlock Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Text="Third row and first cell - Third row and first cell"/>

</Grid>

If you are familiar with Grids in ASP.NET (like GridView, DataGrid etc.), you will notice that Silverlight Grid is little different. In this we first define the Rows and Columns and then define the child elements with Grid.Column and Grid.Row property to place them into respective rows and columns of the Grid.

In the Grid

RowDefinitions > RowDefinition is used to specify the number of rows into the Grid

ColumnDefinitions > ColumnDefinition is used to specify the number of columns into the Grid.

In the above code snippet, we have defined three rows and two columns. In the first two rows we have two cells with TextBlock elements and the last row has only one cell as we have set Grid.ColumnSpan property of the last TextBlock to 2 that spans over last two columns of the Grid.

In case we want an element to span over more than one rows of the Grid, we can set Grid.RowSpan properties as we have done for the Grid.ColumnSpan property.

(In summary, the Columnspan and RowSpan works in almost similar fashion as it works in the HTML table)

Output

TextBlock

TextBlock control is used to display the text. It has some properties/child elements that can be used to write content for this control as well as format the text.

Text

Used to provide value for the TextBlock.

<TextBlock Text="This is simple text" /> 

TextBlock.Text

In case we want to set large amount of text as the value to the TextBlock we can use TextBlock.Text child element and specify its content.

<TextBlock x:Name="myText" Canvas.Top="25">

<TextBlock.Text>

This is the first line

in continuation of first line.

</TextBlock.Text>

</TextBlock>

 

LineBreak

In case we want to write multi-line content to the TextBlock, we can use LineBreak element.

 

<TextBlock x:Name="myText1" Canvas.Top="50">

This is the first line

<LineBreak/>

This is the second line.

</TextBlock>

Run

In case we want to format the content of the TextBlock we can use the Run child element. It has its own attribute that is used to set the foreground color, font style or size.

<TextBlock Canvas.Top="100">

This is simple Text

<Run Foreground="CadetBlue" FontFamily="Georgia" FontSize="15">

This is Georgia font

</Run>

<LineBreak/>

<Run Foreground="DarkGreen" FontFamily="Georgia" FontSize="15">

This is Georgia font in Green color

</Run>

</TextBlock>

 

Output

Hope this article was useful. Thanks for reading, hope you liked it.

Keep reading my forth coming articles on Silverlight. To read my series of articles,click here.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

>> Write Response - Respond to this post and get points
Related Posts

In this article, we would learn the usage of SilverLight UI Controls i.e.TreeView and Button.

Almost everyone doing development in Silverlight is talking about MVVM. This is the part 1 on the subject. I decided to split the articles into different parts as this tends to be a confusing subject for the beginners in the subject. What is good about this article is that I will cover the basics of MVVM and give you a full explanation on how you can retrieve, Edit, add, Update and delete data in MVVM and this is the only article I have seen so far that can do that. Another great thing about this article is that we are not going to use any Toolkit to enforce the rules of MVVM, So this is a Generic MVVM Article, you don’t need to install anything or download anything to follow this article.

In this article I will not be binding the controls with the data from Database. My example is a very simple example, like a hello world type of Example. In this article I am going to bind the individual controls from a property value. To follow my example you need Expression Blend.

In my previous Articles we have learnt “How to start with Silverlight”. In the present article we will learn Silverlight Controls. I am covering the entire Article in a practical view so, I advise you please remind previous articles and create a test application for side-by-side practical or follow these steps to create a new test application:

In this chapter we will first discuss on how to create a new Silverlight project and the structure of ait. Later in this chapter we will discuss on UserControls and various ways of developement. After reading this chapter you will be able to create a HelloSilverlight application and various UserControls for your application.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/27/2012 7:39:37 PM