Introduction
In this article we are going to explore how to bind elements in Windows Phone 7. Further in details we will learn how it is possible to bind elements with others in Windows Phone 7. In this scenario we use an example in which whatever has been entered in the textbox, the text is bound to a TextBlock. You just see the content written inside the TextBox is also written in the TextBox as well. Now Silverlight for Windows Phone 7 data binding enables you to create bindings that allow you to create a linkage between XAML elements. It is called Element Binding. Basically you use element binding when you want to bind to another element's property. It's really useful when you want to connect two objects so that when one object changes the other changes as well. So to do that you should follow the steps given below.
Step 1: In this step first of all we have to open a Windows Phone application; let's see how you will open it.
- Go to Visual Studio 2010
- File->New->Project
- Select the template named as silverlight for Windows Phone
- Select the Windows Phone application
- Give it a name as you want.


Step 2: In this step you see how to bind the Text property of the TextBox which is shown below.
Code:
<TextBox x:Name="textBox"/>
<TextBlock Text="{Binding Text, ElementName=textBox}"/>

Step 3: In this step you just see how to bind the slider value property and the related code is given below.
Code:
<Slider Minimum="20" Maximum="100" x:Name="slider"/>
<TextBlock Text="{Binding ElementName=slider,Path=Value}"/>

Step 4: In this step you just see binding to a custom DependencyProperty of the current page.
XamlCode:
<Button Content="Change DP Value" Click="Button_Click" FontFamily="Comic Sans MS">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FFC8A8A8" Offset="1" />
</LinearGradientBrush>
</Button.Background>
</Button>
<Rectangle Height="200" Width="100" Fill="{Binding Test, ElementName=myPage}"/>
Xaml.cs code:
public static readonly DependencyProperty MyTestProperty = DependencyProperty.RegisterAttached("Test",
typeof(SolidColorBrush),typeof(MainPage), new PropertyMetadata(new SolidColorBrush(Colors.Red)));

Step 5: In this step we will see the complete code for the MainPage.xaml file of Windows Phone 7 application which is given below.
Code:
<phone:PhoneApplicationPage
x:Class="WP7SampleProject14.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait" x:Name="myPage"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">






Comments
Join the conversation! Your thoughts help the community grow.