This article has a corresponding video that can be viewed here.
Master – Detail layouts are the bread and butter of Line of Business (LOB) applications. This walkthrough will show you how to drag & drop from the Data Sources Window to create the UI that is sourced from an Object DataSource.
This article has a C# and VB.NET download that includes both a completed solution and a starter solution. The starter solution includes the data entity classes and a static (Shared) data source class.
This walkthrough assumes that you'll be starting with the starter solution.
Figure 1 Completed Application
Open the accompanying (C# or VB.NET) starter solution.
Figure 2 Starter Solution
The Data class provides two static (Shared) properties that expose an ObserveableCollection that the application will use as its data source. The Data class takes the place of a data layer that would be used to populate CLR classes from a database.
Figure 3 Data Class Diagram
The Customer class can contain one or more Addresses. Each Address has an associated AddressType.
Figure 4 Entity Class Diagram
Figure 5 Draw Border
Figure 6 Reseting Values
Figure 7 Title Step Two
Figure 8 Applying Resource
Figure 9 Auto Sizing Row
Figure 10 StackPanel
Figure 11 StackPanel
Figure 12 Record Selector Completed
From the Data menu, select Show Data Sources.
Figure 13 Data Menu
Figure 14 Data Sources Window
Before clicking the Add New Data Source link, you must have built your solution. The Data Sources Window uses reflection to discover types in your solution. If the type has not been built yet, it will not appear in the Data Sources Window.
Figure 15 Select Data Objects
Figure 16 New Data Source
The ComboBox will provide Customer selection. To wire up the ComboBox drag the Customer object and drop it on the ComboBox as pictured in Figure 17.
Figure 17 Binding ComboBox
Figure 18 Run Application
As part of the Solution Starter, the ApplicationMainWindow Loaded event has code that populates the CollectionViewSource for the application. This is code that developers would normally have to write as the Object Source drag and drop from the Data Sources Window does not add code to your projects.
The details form in this application plays the role of the Master in our Master-Details application.
Customize the Customer object output.
Figure 19 Change Layout Type
Figure 20 Change CustomerID Control
Figure 21 Adding New Row
Figure 22 Details Form Generation
Figure 23 Details Form Completed
Customize the Addresses object output.
Figure 24 Add Grid to Bottom Row
Figure 25 Drag and Drop DataGrid
Figure 26 Data Grid Resized
Edit DataGrid columns using the XAML Editor.
Figure 27 DataGrid Columns
<DataGridComboBoxColumn SelectedValueBinding="{Binding Path=AddressType.AddressTypeId, Mode=TwoWay}" ItemsSource="{x:Static local:Data.AddressTypes}" DisplayMemberPath="Name" Header="Address Type" SelectedValuePath="AddressTypeId" />
See How does it Work section below for an explanation of the DataGrid ComboBox.
The CollectionViewSource is a powerful class that provides a wrapper around a data source, adding sorting, grouping and filtering functionality without changing the source data.
The CollectionViewSource is a proxy class to the underlying CollectionView that provides navigation of collection items.
MSDN CollectionViewSource documentation can be viewed here: http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.aspx
Figure 28 Data Object Relationships
The DataGrid ComboBox displays the AddressType.Name for the Address and provides selection of an AddressType.
Figure 29 DataGrid ComboBox
MSDN DataGridComboBoxColumn documentation can be viewed here:http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn(VS.100).aspx
Microsoft values your opinion about our products and documentation. In addition to your general feedback it is very helpful to understand:
Thank you for your feedback and have a great day,
Karl Shifflett Visual Studio Cider Team
Awesome post Karl. The approach of this post and those that precede it are exactly what is needed for me to get started using Visual Studio 2010, WPF, and Silverlight. Keep them coming!
Hey Karl,
Great article. One question: Where did the resources like "formTitleBackgroundBrush" come from?
Thanks ... bill
Bill,
formTitleBackgroundBrush is in Application.xaml.
Have a great day,
Karl
Thanks for the article.
But I got an error message
"Error 1 Unknown build error, 'The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.' MasterDetailDataSourcesWindowDemo
"
What does mean here? The properties of Window in XMAL is too long?
Hi Karl,
I can use only my:Data.AddressTypes instead of local:Data.AddressTypes in this statement
Why???
The The Aye,
my: and local: are just alias's to the assembly that the types are in.
my: is what the WPF Designer uses when it adds an alias.
local: is what I typically use for the main assembly.
You can change the alias at the top of the XAML file if you want.
Cheers,
Great article. Thanks.
Karl Shifflett has a training module called MVVMTraining.vsix. This add-on does not install or run with Visual Studio 2010 Express.
Supposedly Karl can change the manifest for MVVMTraining.vsix and make it run in VS 2010 Express.
I don't know how to contact Karl and get this problem fixed.
I have to purchase VS 2010 Professional to run his add-on.
Is there a better way?