Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

2. An Introduction to Data Binding > Creating dynamic bindings

Creating dynamic bindings

Creating dynamic bindings Applies to Silverlight 3, 4 and 5 In the previous recipe, you learned how to use data binding in XAML. This is often useful because it allows you to show data easily to your user, for example, showing user information or a list of products. In this recipe, you'll learn how to do exactly the same in C# code instead of XAML. This can be useful in situations where you want to bind a dependency property to the property of an object that you'll know only at runtime. Getting ready For this recipe, we can continue from the solution that was completed in the previous recipe. Alternatively, you can find the starter solution in the Chapter02/SilverlightBanking_Dynamic_Bindings_Starter folder in the code bundle that is available on the Packt website. Also, the completed solution can be found in the Chapter02/SilverlightBanking_Dynamic_Bindings_Completed folder. How to do it... We're going to change the code from the previous recipe, so we can create the bindings in C#, instead of XAML. To do this, we'll carry out the following steps: Open the solution created in the previous recipe, Displaying data in Silverlight applications, locate the grid named OwnersDetailsGrid in MainPage.xaml, and remove the Binding syntax from the XAML code for each TextBlock as shown in the following code: <TextBlock x:Name="OwnerIdValueTextBlock" Grid.Row="1" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="FirstNameValueTextBlock Grid.Row="2" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="LastNameValueTextBlock" Grid.Row="3" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="AddressValueTextBlock" Grid.Row="4" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="ZipCodeValueTextBlock" Grid.Row="5" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="CityValueTextBlock" Grid.Row="6" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="StateValueTextBlock" Grid.Row="7" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="CountryValueTextBlock" Grid.Row="8" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="BirthDateValueTextBlock" Grid.Row="9" Grid.Column="1" Margin="2"> </TextBlock> <TextBlock x:Name="CustomerSinceValueTextBlock" Grid.Row="10" Grid.Column="1" Margin="2"> </TextBlock> Open the code-behind MainPage.xaml.cs file. Here, we're going to create the same bindings in the C# code. In the constructor, after the call to InitializeComponent(), add the following code: OwnerIdValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("OwnerId")); FirstNameValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("FirstName")); LastNameValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("LastName")); AddressValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Address")); ZipCodeValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("ZipCode")); CityValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("City")); StateValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("State")); CountryValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Country")); BirthDateValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("BirthDate")); CustomerSinceValueTextBlock.SetBinding(TextBlock.TextProperty, new Binding("CustomerSince")); A using statement to System.Windows.Data must be added to make this code compile.We can now build and run the application, and you'll notice that the correct data is still displayed in the details form. The result can be seen in the following image: How it works... This recipe shows you how to set the binding using C# syntax. Element.SetBinding expects two parameters, a dependency property, and a binding object. The first parameter defines the DependencyProperty of the element you want to bind. The second parameter defines the binding by passing a string that refers to the property path of the object to which you are binding. There's more... In our example, we've used new Binding("path") as the syntax. The binding object, however, has different properties that you can set and which can be of interest. A few of these properties are Converter, ConverterParameter, ElementName, Path, Mode, and ValidatesOnExceptions. To know when and how to use these properties, have a look at the other recipes in this chapter, and the next recipe will explain all the possibilities in detail. They are, however, already mentioned in this recipe to make it clear that you can do everything that is required as far as bindings are concerned in both C# and XAML.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint