Introduction
This article discusses 12 FAQ's which revolve around bindings, layouts, consuming WCF services and how to connect to database through silver light.
Article first starts with bindings and discusses about one way, two way and one time bindings. Article then moves on to discuss three different ways to layout and position silverlight controls. Finally we end up the article talking about how to consume WCF services in Silverlight and how can we do database operations.
Other Silverlight FAQ's
Silverlight FAQ Part 1:- 21 FAQ's This tutorial has 21 basic FAQ's which will help you understand WPF , XAML , help your build your first silverlight application and also explains the overall silverlight architecture.
SilverLight FAQ Part 2 (Animations and Transformations):- This tutorial has 10 FAQ questions which starts with silverlight animation fundamentals and then shows a simple animated rectangle. The article then moves ahead and talks about 4 different ways of transforming the objects.
Can you explain one way and two way bindings ?
One way binding
As the name so the behavior. In one way bindings data flows only from object to UI and not vice-versa. For instance you can have a textbox called as 'TxtYear' which is binded with an object having property 'Year'. So when the object value changes it will be reflected on the silverlight UI, but the UI cannot update the year property in the object.
It's a three step procedure to implement one way binding. First create your class which you want to bind with the silverlight UI. For instance below is a simple class called as 'ClsDate' with a 'Year' property.
public class clsDate
{
private int _intYear;
public int Year
{
set
{
_intYear = value;
}
get
{
return _intYear;
}
}
}
In the second step you need to tie up the 'Year' property with a silver light UI text box. To bind the property you need to specify 'Binding Path=Year' in the text property of the text box UI object. 'Year' is the property which we are binding with the text box UI object.
<TextBox x:Name="txtCurrentYear" Text="{Binding Path=Year}" Height="30" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center">
</TextBox>
The final step is to bind the text box data context with the date object just created.
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
clsDate objDate = new clsDate();
objDate.Year = DateTime.Now.Year;
txtCurrentYear.DataContext = objDate;
}
}
Two way binding
Two way binding ensure data synchronization of data between UI and Objects. So any change in object is reflected to the UI and any change in UI is reflected in the object.
To implement two way binding there are two extra steps with addition to the steps provided for 'OneWay'. The first change is we need to specify the mode as 'TwoWay' as shown in the below XAML code snippet.
<TextBox x:Name="txtEnterAge" Text="{Binding Path=Age, Mode=TwoWay}" Height="30" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center">
</TextBox>
Second change is we need to implement 'INotifyPropertyChanged' interface. Below is the class which shows how to implement the 'INotifyPropertyChanged' interface. Please note you need to import 'System.ComponentModel' namespace.
public class clsDate : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _intYear; public int Year
{
set
{
_intYear = value;
OnPropertyChanged("Year");
}
get
{
return _intYear;
}
}
private void OnPropertyChanged(string property)
{
if
(PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(property));
}
}
}
The binding of data with data context is a compulsory step which needs to be performed.
Can you explain One time binding?
In one time binding data flows from object to the UI only once. There is no tracking mechanism to update data on either side. One time binding has marked performance improvement as compared to the previous two bindings discussed. This binding is a good choice for reports where the data is loaded only once and viewed.
<TextBox x:Name="txtEnterAge" Text="{Binding Path=Age, Mode=OneTime}" Height="30" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center">
</TextBox>
Can you demonstrate a Simple example of OneWay and TwoWay?
Below is a simple sample code where in we have two text boxes one takes in the age and the other text box calculates the approximate birth date.
Below is a simple class which has both the properties. We have implemented 'INotifyPropertyChanged' interface so that we can have two way communication for the year property.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
// using System.ComponentModel;
namespace SilverLightBinding
{
public class clsDate : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _intYear; private int _intAge;
public int Year
{
set
{
_intYear = value;
OnPropertyChanged("Year");
}
get
{
return _intYear;
}
}
public int Age
{
set
{
_intAge = value;
Year = DateTime.Now.Year - _intAge;
}
get
{
return _intAge;
}
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
Finally we have also binded the SilverLight UI objects with the class properties. Below is the XAML snippet for the same. One point to be noted is that 'Age' is bounded using two way mode as we need to modify the same from the user interface.
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"> Enter your age in the below text box</TextBlock>
<TextBox x:Name="txtEnterAge" Text="{Binding Path=Age, Mode=TwoWay}" Height="30" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBox>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">Your approximate birth date</TextBlock>
<TextBox x:Name="txtCurrentYear" Text="{Binding Path=Year}" Height="30" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBox>
You can download the source code for the same at the end of this article.
What are the different ways provided to do layout in SilverLight?
There are three ways provided by Silverlight for layout management Canvas, Grid and Stack panel. Each of these methodologies fit in to different situations as per layout needs. All these layout controls inherit from Panel class. In the further sections we will go through each of them to understand how they work.
Can you explain how Canvas layout actually works?
Canvas is the simplest methodology for layout management. It supports absolute positioning using 'X' and 'Y' coordinates. 'Canvas.Left' helps to specify the X co-ordinate while 'Canvas.Top' helps to provide the 'Y' coordinates.
Below is a simple code snippet which shows how a rectangle object is positioned using 'Canvas' on co-ordinates (50,150).
<Canvas x:Name="MyCanvas">
<Rectangle Fill="Blue" Width="100" Height="100" Canvas.Left="50" Canvas.Top="150"/>
</Canvas>
Below is how the display looks like. When you the run the code the XAML viewer will position the rectangle object on absolute 'X" and 'Y' coordinates.
How can we implement Grid Layout?
Grid layout helps you position your controls using rows and columns. It's very similar to table defined in HTML.
Below is a simple table with two columns and two rows defined using 'Grid'. We have shown how the table display looks like. Using the 'Grid.ColumnDefinition' we have defined two columns and using 'Grid.RowDefinition' we have defined two rows. We have then created 4 text blocks which are then specified in each section using 'Grid.Column' and 'Grid.Row'. For instance to place in the first column we need specify the 'Grid.Column' as '0' and 'Grid.Row' as '0'. We have followed the same pattern for everyone and you can see all the textblocks are placed in the appropriate sections.
How can we implement Stack Layout?
As the name so the behavior. Stack allows you to arrange your UI elements vertically or horizontally.
For instance below are 4 elements which are arranged in a stack panel element. You can see how the stack aligns / stacks the elements one above other. You can also stack the UI elements horizontally or vertically depending on your layout nature.
You can get a simple source code which demonstrates the three layouts at the end of this article.
What are the different steps involved in consuming WCF service in Silverlight ?
To consume WCF service is a 4 steps procedure.




Gowtham RajamanickamPosted May 20, 2015, 12:58 AM
good show..
Former memberPosted Jul 11, 2009, 7:29 AM
You write like Champ sir. I always find a artsitic way of explanation in your articles. Even if I know the topics , but I can not stop mjy self to read again same topic on your article. Its really Great :)