1.
Problem
You prefer to databind
controls without writing a large amount of code.
2. Solution
Take advantage of the
Silverlight controls that enable codeless databinding in XAML such as
the DomainDataSource control.
3. How It Works
Although the application
services (such as authentication, registration, and profile support)
that the default Business Application template provides are very useful,
WCF RIA Services are all about easily providing data to a Silverlight
application.
In previous recipes, you
performed all databinding in code. In this recipe, you leverage the DomainDataSource XAML control for the presentation tier to
manage data flow in the application.
4. The Code
The first thing you do is open
the Recipe 6 properties dialog and enable the
WCF RIA Services link for the TestWeb project, save the project
settings, and then recompile.
Drag a DomainDataSource
control and a DataGrid on to the
MainPage canvas. You want to display customers from the Northwind database so you name them CustomersDomainDataSource
and CustomersDataGrid, respectively.
To connect the DomainDataSource to the Northwind Domain Service, you need to bring the NorthwindDomainContext
into MainPage.xaml. You do this by
adding a namespace:
xmlns:NorthwindData="clr-namespace:TestWeb.DomainService"
Next, add a resource on to the
page to make the NorthwindDomainContext
available in XAML and then configure your DomainDataSource and DataGrid
as shown in Listing 1:
Listing 1. The Recipe 6
MainPage.Xaml File
<UserControl x:Class="Ch09_LOBApplications.Recipe9_6.MainPage" xmlns:dataControls= "clr-namespace:System.Windows.Controls;assembly= System.Windows.Controls.Data" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:riaControls= "clr-namespace:System.Windows.Controls;assembly= System.Windows.Controls.DomainServices" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:NorthwindData="clr-namespace:TestWeb.DomainService" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.Resources> <NorthwindData:NorthwindDomainContext x:Key="NorthwindDomainContext" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White">
<riaControls:DomainDataSource x:Name="CustomersDomainDataSource" DomainContext="{StaticResource NorthwindDomainContext}" AutoLoad="True" QueryName="GetCustomersQuery" /> <sdk:DataGrid x:Name="CustomersDataGrid" ItemsSource= "{Binding Data, ElementName=CustomersDomainDataSource, Mode=TwoWay}" /> </Grid> </UserControl>
|
To configure the DomainDataSource, you set
the DomainContext to the NorthwindDomainContext
StaticResource. Please note that it is easy to
make a mistake and configure this to the DataContext when configuring databinding. This is incorrect. In order
to have the customer data load properly, you browse the Ch09_LOBApplications.Recipe9_6.Web.g.cs
code generated file to find the method GetCustomersQuery and
configure that on the QueryName property. You also tell the
control to AutoLoad by setting that property to True.
To configure the DataGrid, you databind its ItemSource
property to the Data property on the CustomersDomainDataSource
object. Running the code results in Figure 1.