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.
Christian
Christian,
Do you know there is already a WPF counterparty to System.Windows.Forms.BindingNavigator?
Cheers.
Posted by: Luciano Evaristo Guerche (Gorše) | 08/05/2010 at 01:59 PM
I guess something like the Windows Forms BindingNavigator is here with Visual Studio LightSwitch http://weblogs.thinktecture.com/cnagel/2010/08/data-centric-silverlight-applications-with-visual-studio-lightswitch.html (with Silverlight). And it's not too hard to do that with WPF and the Ribbon control :-)
Cheers
Posted by: Christian | 08/06/2010 at 04:28 PM
Luciano, I've done a sample of a BindingNavigator for WPF - see here: http://weblogs.thinktecture.com/cnagel/2010/08/bindingnavigator-for-wpf-part-1---creating.html
Christian
Posted by: Christian | 08/20/2010 at 08:34 PM
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.
Posted by: r4 | 10/17/2011 at 06:22 AM