Blend For Visual Studio 2012 by Example: Beginner's Guide - Second Edition - Sample Chapter
Blend For Visual Studio 2012 by Example: Beginner's Guide - Second Edition - Sample Chapter
Blend for Visual Studio 2012 by Example Beginner's Guide will give you a good grounding in creating
Windows, Web, and Windows Phone applications. You will also look at the various layouts and controls
available in Blend and how we can create animations and behaviors in Blend.
Towards the end, you will be aware of the various capabilities that are available in Blend out of the box.
useful tasks
Sa
m
pl
e
experimentation
Abhishek Shukla
$ 54.99 US
34.99 UK
"Community
Experience
Distilled"
ee
Creating applications with compelling graphics has been one of the main goals of client applications,
and with the arrival of WPF, Silverlight, and HTML5 it is much easier than ever before to create interactive
and rich user interfaces.
Beginner's Guide
Abhishek Shukla
Preface
Preface
Creating applications with compelling graphics has been one of the main goals of client
applications, and with the arrival of WPF, Silverlight, and HTML5, it is much easier than ever
before to create interactive and rich user interfaces. These technologies make use of the
computational and graphical power of computers.
This book is a hands-on guide that provides you with a number of clear, step-by-step exercises
that will help you take advantage of the real power of Blend in creating WPF, Silverlight,
and HTML5 applications. It will give you a good grounding in creating Windows, web, and
Windows Phone applications. You will learn about the various tools and techniques that are
available in Blend and the dierent types of applications that we can create using Blend.
By the end of the book, you will be well aware of all the major concepts in Blend and will
also be able to develop Windows, web, and Windows Phone applications. You will also be
aware of the various capabilities that are available in Blend out of the box.
Preface
Chapter 4, Styles and Templates, teaches you what styles and templates are and how you
can create, modify, and reuse them in Blend.
Chapter 5, Behaviors and States in Blend, shows you how you can attach behaviors and
actions to elements and how you can use visual states in your applications.
Chapter 6, Understanding Animation and Storyboards, shows you how to create animations
in Blend, modify them, and use them in your applications. This chapter also covers how you
can create and design storyboards in Blend.
Chapter 7, Understanding DataBinding, covers DataBinding and how it works.
Chapter 8, Vector Graphics, provides you with the understanding of vector graphics and
has a look at how it's dierent from normal graphics. You will see how you can create
vector graphics and advantages of using it.
Chapter 9, User Controls and Custom Controls, teaches you what user controls and custom
controls are and how you can create and reuse them.
Chapter 10, Creating Windows Phone Apps, shows you how you can design and develop
Windows Phone applications from Blend itself.
Chapter 11, Creating Windows 8 Store Apps, shows you how you can design and develop
Windows 8 Store applications from Blend itself.
Chapter 12, Prototyping Using SketchFlow, a bonus chapter, looks at what SketchFlow is
about and how it helps you in prototyping designs and getting quick and usable feedback.
You can download it from https://www.packtpub.com/sites/default/files/
downloads/3882OT_Chapter12.pdf.
Layout Panels
A good tool is one that offers a combination of great controls and makes it easy
to bring them together to build an amazing user interface.
In Chapter 1, Getting Started with Blend, we installed Blend and familiarized ourselves with
the Blend IDE. In this chapter, we will take a look at the various layout panels.
Layout panels, including the one shown in the following screenshot, are components
that control the rendering of their children, including the size, dimensions, position, and
arrangement of their child content. All panels support the sizing and positioning properties
of FrameworkElement. The FrameworkElement class provides the set of properties,
events, and methods for WPF elements, and all the panels derive from FrameworkElement.
There are primarily five panels available in WPF:
Grid: This is represented by in the tools panel. It arranges its child controls in a
flexible layout of rows and columns forming a grid.
Layout Panels
Only the Grid, Canvas, StackPanel, and ScrollViewer panels are available in a
Silverlight project.
Grid
The grid layout panel arranges its child controls in a tabular structure of rows and columns.
The grid layout panel allows us to position and style elements easily. This layout panel helps
us in structuring our application in the form of a row-and-column layout format.
We have added a few controls in the grid layout panel to make it look like the run command
available in Windows. Here's how it looks:
[ 24 ]
Chapter 2
When we don't explicitly define any rows or columns, even then the grid
layout has RowDefinition and ColumnDefinition. This takes up the
entire space inside the grid layout in one cell.
When we place more than one element in the same cell, they might end up
overlapping as the grid inherently does not have any mechanism to stack or
queue items.
1.
2.
3.
Hover the mouse just above the grid area, and you will notice the vertical yellow
line that appears with a header. This represents the column that will be added to
the grid layout if we click on the grid layout. We will see a similar yellow line, but
horizontal, when we move the mouse just left of the grid area. This represents the
row that will be added to the grid layout if we click on the grid layout.
Right-click on the project name and select Add New Item. Add a new window
to the project and name it GridLayout.xaml. This is shown in the following
screenshot:
[ 25 ]
Layout Panels
4.
Add four columns and three rows to the grid layout, as shown in the following
image:
While adding columns, if the divider does not land up where we want it, then we
can hover the mouse above the column divider, and the cursor will change to show
the column divider to be moved. Also, when we hover the mouse just above the
grid area within a column, we will have multiple options to change the width of the
column as a ratio using *, define a fixed width using pixels, or set the width to Auto.
This will change the width according to the content of that column. This is discussed
further in the following list:
Star: This specifies the width of the column relative to the other columns.
For example, if we specify the width of column 1 as 1* and then the width
of column 2 as 2*, then column 2 will have twice the width compared
to column 1. And, when we specify the width of a column as *, then it
occupies the remaining horizontal space inside the grid layout.
Pixel: This fixes the width of the column to be the same as the specified
pixels.
[ 26 ]
Chapter 2
Auto: This adjusts the width of the column according to the content of the
column. The following set of screenshots shows this:
5.
Select LayoutRoot from the Objects and Timeline panel, and, in the Properties
panel, set Height and Width of the grid layout as 450 and 175 respectively. This will
make the grid layout match the size of the run command.
6.
7.
After defining the grid layout's row and columns, we will add some content in the
grid layout.
[ 27 ]
Layout Panels
8.
Go to the text icon in the tools panel and press the left button of the mouse for a
few milliseconds and we will see the flyout menu to select the text tool.
9.
Select the TextBlock option and draw TextBlock in the grid layout by moving the
mouse to the first column and then pressing the left button of the mouse and
dragging the mouse without releasing the mouse button. Then, release the mouse
once TextBlock of the desired size is created. The following screenshot shows this:
10.
Select the TextBlock option, move to the Properties panel, and make sure that we
have Row and Column set to 0 and 1 respectively.
11.
Also, we have RowSpan set to 1 and ColumnSpan set to 4. RowSpan specifies the
number of rows the element will take, and ColumnSpan specifies the number of
columns the element will take.
12.
[ 28 ]
Chapter 2
13.
With TextBlock selected in the Properties panel, set the text of TextBlock to Type
the name of a program, folder, document, or Internet resource, and Windows will
open it for you. This is shown in the following screenshot:
14.
Now, let's create TextBlock in the first row and zeroth column. Also, we will change
the text of TextBlock to Open:
15.
Now, we will go back to the tools panel and select TextBox, this time from the text
controls, and add it to the first column and first row of the grid layout. Then, we
will change the ColumnSpan property of TextBox to span through four columns.
We will also change the Background brush of TextBox to a shade of gray. This is
incorporated in the following screenshot:
[ 29 ]
Layout Panels
16.
From the tools panel, we will now select buttons and add a button in each of the
second, third, and fourth columns of the third row:
17.
Select each of the buttons one by one, and change the text of the buttons to Ok,
Cancel, and Browse, and the GridLayout window will look similar to the run
command of the window:
Chapter 2
We see that the height and width of the line are specified by a number followed by *. This
means we are not specifying a fixed width or height as we always do; we are specifying the
height and width as a ratio. So, the height of the four rows will be in the ratio 1:1:2:1.5.
We can also specify a fixed height, and, if we don't specify any height, then the height of
the five columns will be in the ratio 1:2:2:2:2. This discussion is encapsulated in the
following code:
<Grid x:Name="LayoutRoot" Width="450" Height="175"
HorizontalAlignment="Left" VerticalAlignment="Top"
Background="White">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="2*"/>
<RowDefinition Height="1.5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
</Grid>
Have a go hero
Whenever you add rows or columns to your grid layout, make sure that the ShowGridLines
property of the grid layout is either not set or set to false. This will ensure that the border
between the rows and columns will be invisible at runtime. If you want these to be visible at
runtime, go ahead and set ShowGridLines = true.
[ 31 ]
Layout Panels
Canvas
The canvas layout panel is another commonly used layout control. As opposed to a grid
layout, a canvas layout has no columns or rows, and all controls must be absolutely
positionedprovided as osets from the edges of the canvas layout. But, the elements can
still overlap inside the canvas layout as in the case of the grid layout. This means that we can
specify the position of an element on the canvas by specifying the top, left, right, and bottom
properties of the element. These properties specify the position of the element from the
top, left, right, and bottom walls of the canvas layout respectively. The canvas layout is the
only layout that allows you to have an absolute coordinate for each object.
1.
Right-click on LayoutRoot (grid) and select Change Layout Type | Canvas. This
feature is provided by Blend to easily switch between the various layout types. The
following screenshot shows this:
When we select any of the controls, we would notice the left and top
properties (as opposed to the Grid.Row and Grid.Column properties in
the grid layout). The following screenshot shows this:
[ 32 ]
Chapter 2
2.
We can also achieve the same positioning that we have using margins instead
of using Canvas properties or a combination of both. For example, let's set the
Canvas.Left and Canvas.Top values of the Ok button to 0 and the Margin.
Left and Margin.Top values to the values that were set for left and top. This
is what Blend does when we switch the layout from canvas to grid, to keep the
element positioning same. The following screenshot shows this:
3.
If we want to reposition any of the elements, we simply select and drag them, and
Blend automatically changes the Canvas.Top and Canvas.Left properties.
StackPanel
StackPanel is used to stack elements one after another either vertically or horizontally.
[ 33 ]
Layout Panels
1.
To see how it works, let's go ahead and add a new window to our project, and
let's name it StackPanelLayout.xaml. Go to the Assets panel and search for
StackPanel. Then, drag and drop it onto LayoutRoot.
2.
Now, search for the buttons in the Assets panel, and drop three of them onto
StackPanel. You will see that, as you add these buttons onto StackPanel, they are
added vertically one after another. You can set a height, width, and color for these
buttons. Make sure that your code looks similar to the one shown in the following
screenshot:
3.
We can also arrange the elements with StackPanel horizontally by setting the
Orientation property of StackPanel to Horizontal, as shown here:
[ 34 ]
Chapter 2
DockPanel: DockPanel could be used to describe the overall layout of a simple user
interface. DockPanel arranges its children so that each of them fills a particular edge
of the panel. If multiple children are docked to the same edge, they simply stack
up against that edge in order. This provides an easy docking of elements to the left,
right, top, bottom, or centre of the panel. The dock side of an element is defined by
the attached property DockPanel.Dock. To fill the remaining space of the panel,
we make the LastChildFill property, of the panel, to true.
[ 35 ]
Layout Panels
[ 36 ]
Chapter 2
Pop quiz
Q1. How can we show the border lines of the grid layout?
1. ShowGridLines = "True".
2. GridLine = "True".
3. NoGridLines = "False".
4. ShowGridLine = "True".
Q2. What are the various layout panels in WPF?
1. StackPanel & Grid.
2. Canvas.
3. DockPanel & Wrap Panel.
4. All.
Summary
In this chapter, we looked at various layout panels, including Grid, Canvas, StackPanel,
WrapPanel, DockPanel, and ViewBox.
In the next chapter, we will take a look at XAML and help you understand how it works.
[ 37 ]
www.PacktPub.com
Stay Connected: