WPF and Data
For any kind of data handling tasks, you first need to have data stored somewhere so it can be retrieved. In .NET applications, a data source holds the data that can be used within your application.
WPF is typically known as a technology used to create visually rich applications. WPF, however, is much more than just a rich user interface generator. Developers across the world are now employing WPF to create powerful business applications. These applications deal extensively with data in some form or the other.
WPF provides strong support and powerful features for data access. WPF 4, together with the Visual Studio 2010 IDE, helps you develop data-bound applications efficiently.
Data Sources
WPF enables you to work with various kinds of data source types such as:
- ADO.NET Database
- Data from a Service
- Data from Objects in an assembly
- Data from a SharePoint site
The Visual Studio IDE now enables you to create any of the above four data source types using a Data Source Configuration Wizard. Note that the above four types are supported only in 2010, earlier versions of the IDE supported fewer data source types.
The ADO.NET Database data source type enables you to create data objects by using any one of two data models: A Dataset or an Entity Data Model. A Dataset is one of the most common data models and is used to store and retrieve data from a data source such as an SQL Server database.Depending on your requirements, you can also change the data source to an MS Access database, ODBC data source, SQL Server 3.5 Compact database, and so forth. Entity Data Model creates data classes based on the ADO.NET Entity Data Framework.
Object Data Sources
You can also use data from objects in your assembly as a data source. Typically, this is a object of type user-defined class. The class defines the data that can be used by a WPF application. For example, you could have a class defining a List collection populated with strings and then using the Object option in the Choose Your Data Source Type screen of the Data Source Configuration Wizard, you can add the class as a data source.
Then you can create a static resource for it using the ObjectDataProvider class in XAML and make use of this resource for data binding. Alternatively, you could directly reference the class object in code and use DataContext to bind to it, without any mention of it in XAML.
Example
Let's see the first approach in action. We will take an example of a WPF application that will bind to a list of car manufacturers. The data source will be list and the data will be the names of the car manufacturers.
Launch Visual Studio 2010 and select WPF application in the New Project dialog box. Name the project as ObjectDataSource. Drag and drop a ListBoxc control from the Toolbox.
The resulting XAML code for this would be as shown:
Next, we will create the class which will define the data for the data source. Select Project->Add Class and name the class file as Cars.cs. Add the following code in it.
Here, we have declared a method CreateList() inside the class Cars. This method returns a generic collection of strings. Within the method, we create an instance of a List of strings named manufacturers. Then, we use a series of Add() methods to populate the List with data. The newly populated list will be returned from the method.
Now we will see how create a static resource for the class using ObjectDataProvider in XAML. The ObjectDataProvider class enables you to create your object in XAML and make it available as a binding source. It is defined in the System.Windows.Data namespace. This class provides the following properties:
ConstructorParameters property: Allows you to pass parameters to the constructor of your object
MethodName property: Allows you to call a method
MethodParameters property: Allows you to pass parameters to the method
Using one or more of the above properties, you can then bind to the results of the method.
Let's make use of ObjectDataProvider in our code.
As you can see, we have used the MethodName property to indicate that we want to bind to the results of this method.
We will now use databinding to bind to this resource and also customize the look of the ListBox using ItemTemplate and DataTemplate. We use the ItemsSource property of the ListBox to bind to the static resource.
If required, in addition to the above steps, you can also set the DataContext of the ListBox in code to bind to the object. Here, in the current scenario, this is optional.
So far, so good. A simple example for a simple scenario. But what if your class was a bit more complex and you had to pass parameters to process the data? Let's see how we can deal with this. First, we add more spice to the class.
Then we specify the MethodParameters property in XAML.
The complete XAML listing is as follows:
Notice that in the namespace declaration section, we have included the System namespace.
This has been done because we have used a parameter of string type. This type is not recognized by default in XAML. Hence you need to reference the assembly for it. Also, note that though we have used a single parameter in this example, you can specify any number of parameters you want. Each of the parameters must match in type to the parameters declared in the class method definition. Also, you can pass parameters of several different types together.
One other property of ObjectDataProvider class that can be useful is the IsAsynchronous property. This property is used to indicate whether the object creation is to take place in a worker thread or in the active context.
Though this article primarily discussed binding to methods of objects, you can also bind to constructors of objects through the ObjectDataProvider class. You can try that out yourself, the procedure for it is almost similar to that described above.
Summary
In this article, you learnt how to bind to a CLR object in WPF.
About Medusa M
 |
Medusa loves experimenting with technology trends, especially that of Microsoft.
This author has published 14 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
November's Toolbox Column Now Online
read more
RadScheduler for WinForms data binding and occurrence exceptions
read more
Introducing Versatile DataSources
read more
How to display data from different tables using one data source
read more
ASP.NET 4 Beta 2 - New Version, New Docs, New MSDN Site !
read more
Building a class browser with Microsoft Ajax 4.0 Preview 5
read more
ASP.NET AJAX 4.0 Preview 5 released to CodePlex
read more
5 Minute Overview of MVVM in Silverlight
read more
Chat room questions from the EF Tips & Tricks webcast
read more
Self-reference hierarchy with Telerik TreeView for Silverlight
read more
|
|
Please login to rate or to leave a comment.