WP7 Element Binding samples
published on: 2/16/2011 | Views: N/A | Tags: Binding
by WindowsPhoneGeek
In this post I am going to talk about Elemen Binding in Silverlight for Windows Phone 7.
What is Element Binding?
Silverlight for Windows Phone 7 data binding enables you to create bindings that allow you to create a linkage between XAML elements. This 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. To use element binding, you can specify the ElementName as part of the binding syntax :
Text="{Binding TargetValue, ElementName = TargetElementName}"
or
Text="{Binding ElementName = TargetElementName, Path = TargetValue}"
Where Text is a sample dependency property.
When you set ElementName property, the specified value must refer to an element in one of the following locations:
-
The current XAML namescope.
-
The XAML namescope of the templated parent if the binding target is in a data template or control template.
Because of this restriction, you cannot use the ElementName property to bind to elements that are not created by using XAML. To bind to elements created programmatically, use the Source property instead.
Source , RelativeSource, and ElementName are mutually exclusive in a binding. If you have set one of these attributes, then setting either of the other two in a binding (through XAML or through code) will cause an exception.
Examples
Example1: Binding to TextBox Text property.
<TextBox x:Name="textBox"/>
<TextBlock Text="{Binding Text, ElementName=textBox}"/>
Example2: Binding to Slider Value property
<Slider Minimum="20" Maximum="100" x:Name="slider"/>
<TextBlock Text="{Binding Value, ElementName=slider}"/>

Example3: Binding to a custom DependencyProperty of the current page.
<Button Content="Change DP Value" Click="Button_Click"/>
<Rectangle Height="200" Width="100" Fill="{Binding Test, ElementName=myPage}"/>
public static readonly DependencyProperty TestProperty = DependencyProperty.RegisterAttached( "Test", typeof(SolidColorBrush), typeof(MainPage), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
public SolidColorBrush Test
{
get { return (SolidColorBrush)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Test = new SolidColorBrush(Colors.Green);
}
}
You can find the source code here.
I hope that the tip was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
