Home   Subscribe   Linkedin
  Archive Contact  

How to Bind Data to a Property in Silverlight

In this article i am going to describe how to Bind Data to a Property in Silverlight in some simple steps.

Step 1: Create a new Silverlight Application Project, give it a name say BindingInSilverlight.

Step 2: Go to MainPage.xaml and prepare UI.

Initially xaml inside it will look like this:

<UserControl x:Class="BindingInSilverlight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="500" d:DesignWidth="800">

    <Grid x:Name="LayoutRoot" Background="White" Margin="50,20,0,0">

    </Grid>
</UserControl>

Now add RowDefinitions and ColumnDefinitions in Grid tag,

it will give a better look and feel to our layout.

<Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="150"/>
        </Grid.ColumnDefinitions>

Add some Controls in it like TextBlock and TextBox with Grid.Row and Grid.Column specifications for accurate layout.

Add Binding paraeters: Name and Place are two Properties to Bind here.

<TextBlock Grid.ColumnSpan="2" Grid.Row="0" Grid.Column="0" Text="Insert Values to Bind"
 FontSize="16" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Grid.Row="1" FontSize="14" Grid.Column="0" Text="Name :" VerticalAlignment="Center"/>
        <TextBlock Grid.Row="2" FontSize="14" Grid.Column="0" Text="Place To Visit:" VerticalAlignment="Center"/>
        <TextBox FontSize="14" Text="{Binding Name, Mode=TwoWay}" Grid.Row="1" Grid.Column="1" Margin="5"/>
        <TextBox FontSize="14" Text="{Binding Place, Mode=TwoWay}" Grid.Row="2" Grid.Column="1" Margin="5"/>
        
        <TextBlock Grid.ColumnSpan="2" Grid.Column="3" Grid.Row="0" Text="Binding..." FontSize="16" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Grid.Row="1" FontSize="14" Grid.Column="3" Text="Hello:" VerticalAlignment="Center"/>
        <TextBlock Grid.Row="2" FontSize="14" Grid.Column="3" Text="Welcome to:" VerticalAlignment="Center"/>
        <TextBlock FontSize="14" Text="{Binding Name}" Grid.Row="1" Grid.Column="4" Margin="5"/>
        <TextBlock FontSize="14" Text="{Binding Place}" Grid.Row="2" Grid.Column="4" Margin="5"/>

Finally our UI will look like this:

Left Side to get Data from User, and Right side to show binding.

 

Step 3: Go to MainPage.xaml.cs page, now we have to add some Code there.

First of all implement INotifyPropertyChanged interface with namespace System.ComponentModel,

the INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

Also define Properties Name and Place with their get and set methods along with a OnPropertyChanged method,

this will be called when it notifies some change in the Property.

After doing all our MainPage.xaml.cs will look something like this:

using System.ComponentModel;

namespace BindingInSilverlight
{
    public partial class MainPage : INotifyPropertyChanged                         // INotifyPropertyChanged interface.
    {
        public event PropertyChangedEventHandler PropertyChanged;                  // PropertyChanged event 

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(property));            // Notification.
            }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private string _place;
        public string Place
        {
            get { return _place; }
            set
            {
                _place = value;
                OnPropertyChanged("Place");
            }
        }

        public MainPage()
        {
            DataContext = this;
            InitializeComponent();
        }
    }
}

Step 4: Start filling Details.

you will see whenever you navigate away from any TextBox its contents are automatically

visible in the Right hand side of the Layout as:

This is how we can Bind Data to a Property in Silverlight.

Posted by: Arun Verma

Categories: Silverlight

Tags: ,

Why SharePoint?

Need:

Microsoft SharePoint 2010 makes it easier for people to work together.

Using SharePoint 2010, your people can set up Web sites to share information with others, manage documents from start to finish, and publish reports to help everyone make better decisions.

Basically Any Organization  uses SharePoint for:

1>Share data.

2>Communication.

3>Collaboration.

The main Feature of SharePoint are as following :

1> Sites: SharePoint 2010 sites provides a single infrastructure for all your business websites , share  documents with collegues ,manage projects with partners and publish information to customers.

2> Composites :SharePoint composites offers tools and component for creating do-it-yourself business solution build no code solution to rapidly respond to business needs.

3> Insights: SharePoint Insights gives everyone access to the information in databases,reports,ans business application .help people locate the information they need to make good decisions.

4>Communities: SharePoint communities delivers great collaboration tools and a single platform to manage them .Make it  easy for people to share ideas and work together the way they want

5> Content: Sharepoint Content makes content management easy .Setup compliance measures ”behind the scenes” with features like document types,retention policies ,and automatic content sorting and then let people work naturally in Microsoft Office .

6>Search :Sharepoint Search Cuts through the clutter . A unique Combination of relevance , refinement ,and social cues helps people find the information and contacts they need to get their jobs done.

The Main Features of SharePoint :

1>Easily Create a Collaborative Site.

2>Efficiently Manage Information.

3>Facilitate Team Collaboration .

4>Enhance Communication .

5>Automate Business Processes.

6>Generate Relevant Reports .

Posted by: Ashwani

Categories: SharePoint

Tags: