Cancellation with .NET 4 (Part 2)
Data-Centric Silverlight Applications with Visual Studio LightSwitch

Data-Centric WPF Applications with Visual Studio 2010

Data Sources from Visual Studio 2010 allows for easily creating user interfaces for WPF applications to read/write data. Previously, with Visual Studio 2008 this was only available for Windows Forms applications, and used the DataSet behind the scenes. Now WPF is supported as well, and an Entity Framework can be used.

After creating a WPF application, a data source can be created. The data source that can be selected from the Data Source Configuration Wizard allows accessing  Database, a Service, an Object, and SharePoint.


After selecting the data source either the old DataSet can be used for accessing data, or a Entity Data Model can be created.


The Data Sources window now allows if data should be displayed within a DataGrid, as list or detail information. Then it’s possible to define the UI elements for each property of the DataSource, e.g. Label, TextBox, ComboBox, TextBlock… For a DateTime data type per default the DatePicker is selected. It’s also possible to add custom controls.


By drag&drop from the Data Sources window to the WPF designer the UI elements are added to the XAML code. The UI elements that are added are data-bound to CollectionViewSource elements. CollectionViewSource is a proxy to CollectionView that itself allows grouping, sorting, filtering, and navigation.


<Window.Resources> <CollectionViewSource x:Key="racersViewSource" d:DesignSource="{d:DesignInstance my:Racer, CreateList=True}" /> <CollectionViewSource x:Key="racersRaceResultsViewSource" Source="{Binding Path=RaceResults, Source={StaticResource racersViewSource}}" /> </Window.Resources>


In the code-behind generated code a query is created across the object context to retrieve data from the database.


private ObjectQuery<Racer> GetRacersQuery(Formula1Entities formula1Entities) { // Auto generated code ObjectQuery<Racer> racersQuery = formula1Entities.Racers; // Update the query to include RaceResults data in Racers. You can modify this code as needed. racersQuery = racersQuery.Include("RaceResults"); // Returns an ObjectQuery. return racersQuery; }


This query is used when loading the Window to assign the Source property of the CollectionViewSource.


private void Window_Loaded(object sender, RoutedEventArgs e) { Formula1Entities formula1Entities = new Formula1Entities(); // Load data into Racers. You can modify this code as needed. CollectionViewSource racersViewSource = ((CollectionViewSource)(this.FindResource("racersViewSource"))); ObjectQuery<Racer> racersQuery = this.GetRacersQuery(formula1Entities); racersViewSource.Source = racersQuery.Execute(MergeOption.AppendOnly); }


The generated code might not be the one exactly needed. For very simple scenarios it just might be enough and just needs small tweaks and additions to write data to the database.

In most scenarios the code needs bigger changes and additions. This technology still might be a good use for a start to create UI elements based on some data and change it afterwards. The generated XAML code for the UI can also be copied to different projects.



Feed You can follow this conversation by subscribing to the comment feed for this post.

Luciano Evaristo Guerche (Gorše)


Do you know there is already a WPF counterparty to System.Windows.Forms.BindingNavigator?



I guess something like the Windows Forms BindingNavigator is here with Visual Studio LightSwitch (with Silverlight). And it's not too hard to do that with WPF and the Ribbon control :-)



Luciano, I've done a sample of a BindingNavigator for WPF - see here:



Thank you for you efforts looking for this great list. Welcome to the do follow community I am hoping for a great work from you in the future. Do follow Blogs is so helpful for getting better page rank and traffic.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)