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 single download that contains C# and VB.NET completed solutions and starter solutions. The starter solution includes the data entity classes and a static (Shared) data source class.
This walk through 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 are the application data sources. 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 Top Border
Figure 6 Resetting Values
Figure 7 Title Step Two
Figure 8 Applying Resource
Figure 9 Auto Sizing Row
Figure 10 Record Selector Step One
Figure 11 Record Selector Step Two
Figure 12 Record Selector Completed
From the Data menu, select Show Data Sources.
Figure 13 Data Menu
Figure 14 Data Sources Window
Figure 15 Select Data Objects
Figure 16 New Data Source
The ComboBox will provide Customer record 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
VB.NET
Imports System.Windows.Data Imports System.ComponentModel Partial Public Class MainPage Inherits UserControl Public Sub New() InitializeComponent() End Sub Private Sub UserControl_Loaded( ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded If Not (DesignerProperties.GetIsInDesignMode(Me)) Then Dim cvs As CollectionViewSource = CType(Me.Resources("CustomerViewSource"), CollectionViewSource) cvs.Source = Data.Customers End If End Sub End Class
C#
using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace MasterDetailDataSourcesWindowDemo { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { if (!DesignerProperties.GetIsInDesignMode(this)) { CollectionViewSource cvs = (CollectionViewSource)this.Resources["CustomerViewSource"]; cvs.Source = Data.Customers; } } } }
Figure 18 Run Application
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 Customer Layout Type
Figure 20 Change CustomerID Layout Control
Figure 21 Adding New Row
Figure 22 Details Form Generation
Figure 23 Details Form Completed
Customize the Addresses object output
Figure 24 Configure CustomerID Layout Control
Figure 25 Add Grid to Bottom Row
The reason we have added the Grid is to provide a surrounding container for the DataGrid we will add in the next step.
The Grid container will have its DataContext property set in the next step when we drag and drop the Addresses DataGrid onto the design surface.
If we didn't add this Grid container, the Data Sources Window would look up the tree of elements trying to locate a container to set the DataContext on. In the case of this application, the DataContext would be set on the top level Grid control. While this would work, it would make the XAML harder to read and understand where the DataGrid's DataContext was set.
By placing the DataGrid inside its own surrounding Grid control, it's very easy to understand where the DataGrid's DataContext is when reading the XAML.
Figure 26 Drag and Drop Addresses DataGrid
Figure 27 Data Grid Resized
We now need to add an AddressType ComboBox column and wire it up to data source.
<local:Data x:Key="data" />
<sdk:DataGrid.Columns>
The WPF & Silverlight Designer currently does not support the editing DataTemplates so you'll need to edit the XAML in the XAML Editor.
However, when editing in the XAML Editor, if you have loaded the Designer, you can use still use the Properties Window to set property values on the selected object.
<sdk:DataGridTemplateColumn x:Name="AddressTypeColumn" Header="Address Type" Width="100"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Margin="3,0,0,0" VerticalAlignment="Center" Text="{Binding Path=AddressType.Name}" /> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> <sdk:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ComboBox DisplayMemberPath="Name" ItemsSource="{Binding Path=AddressTypes, Source={StaticResource data}}" SelectedItem="{Binding Path=AddressType, Mode=TwoWay}" /> </DataTemplate> </sdk:DataGridTemplateColumn.CellEditingTemplate> </sdk:DataGridTemplateColumn>
Figure 28 After Pasting Address Type Column
Figure 29 DataGrid Column Collection Editor
The DataGridColumn Width property is of type DataGridLength. Star sizing for the Silverlight DataGrid is a new feature in Silverlight 4.
You can read about the DataGridLength in this MSDN topic:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridlength(VS.95).aspx
Figure 30 DataGrid Column Reordering
Figure 31 DataGrid After Column Resizing
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(VS.95).aspx
Figure 32 Data Object Relationships
Silverlight does not ship with a DataGridComboBoxColumn class. Instead, it provides the flexible DataGridTemplateColumn class. This column exposes two template properties, CellTemplate and CellEditingTemplate that enable developers to author DataTemplates when the cell is in read or edit mode.
MSDN DataGridTemplateColumn documentation can be viewed here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn(VS.95).aspx
Now that you have a built a simple Silverlight application can we suggest that you view Scott Morrison's MIX10 presentation, building Silverlight Business Applications.
Presentation: http://live.visitmix.com/MIX10/Sessions/CL08
Blog post with source code: http://blogs.msdn.com/scmorris/archive/2010/03/17/mix-10-silverlight-4-business-applications.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 Expression Team
After reading this article and other series related to Visual Studio 2010 I'm finally convinced that I could install VS2010 and using wpf instead of WinFormfor my next
project at work. Thanks, keep new articles like this (WPF and how easy VS2010 helps developers) coming.
Thank you very much for your kind words of encouragement.
Have a great day,
Karl Shifflett
hello, in my Data Sources Panel, i lost DataGrid in my tables and i only have Details, how to retrieve DataGrid ? thks
Is your Designer visible? If not, the Data Sources Window changes its mode.
Let me know if this does not correct you issue.
Cheers,
Karl
Hi, I have also lost the DataGrid from the data sources. I have the designer open.
I only have the options of Details and DataForm. If I go to customise DataGrid is not available enywhere.
If I drag a DataGrid from the toolbox, then deag my datacontext onto it, it all populates but I dont get to control which items are included.
Visual Stuiod 2010 & Nigel,
Question: have you installed the latest Silverlight Tools from here: www.microsoft.com/.../details.aspx
Please email me a screen shot what you are seeing and your project to:
kashiffl AT microsoft dot com
We'll get this sorted out for you,
How to add navigation cabaplties .add ,save To this sample
Mohd,
Thank you for your feedback. To help with the "add and save" features, it would be helpful to know if you are using WPF or Silverlight?