MULTIMEDIA

Building LOB Applications : Databinding in XAML

2/24/2011 5:50:36 PM

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.

Figure 1. No-code XAML databinding in action

Other  
  •  Microsoft XNA Game Studio 3.0 : Program Bugs
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Adding Vibration
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Using the Keyboard
  •  iPhone 3D Programming : Blending and Augmented Reality - Stencil Alternatives for Older iPhones
  •  iPhone 3D Programming : Blending and Augmented Reality - Poor Man’s Reflection with the Stencil Buffer
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
  •  iPhone 3D Programming : Blending and Augmented Reality - Shifting Texture Color with Per-Vertex Color
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Extensions and Their Uses
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Caveats
  •  Building LOB Applications : Using Visual Studio 2010 WCF RIA Data Services Tooling
  •  Building LOB Applications : Implementing CRUD Operations in WCF Data Services
  •  iPhone 3D Programming : Blending and Augmented Reality - Wrangle Premultiplied Alpha
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 3)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 1) - Games and Classes & Classes as Offices
  •  Microsoft XNA Game Studio 3.0 : Working with Colors
  •  iPhone 3D Programming : Textures and Image Capture - Creating Textures with the Camera
  •  iPhone 3D Programming : Textures and Image Capture - Dealing with Size Constraints
  •  Programming with DirectX : Game Math - Vectors
  •  
    Most View
    HTC Windows Phone 8X - The Best-Looking Windows Phone 8 Handset (Part 1)
    The big test … Inter Core Power (Part 1) - Acer Aspire Ethos 8951G
    App attack: TV Remotes - Panasonic Viera, LG TV Remote, Media Remote (Sony) & Samsung Remote
    Group Test: Integrated Valve Amps $2,175-$3,000 (Part 3)
    iPhone 5 - First Among Equals
    Iphone SDK : Working with the Address Book Database - Property Types, Accessing Single-Value Properties
    Samsung NX210 – Snap N Share
    Integrating Office Communications Server 2007 in an Exchange Server 2010 Environment : Web Conferencing
    Windows Server 2008 and Windows Vista : Getting Around in the GPMC (part 1) - Forest Views in the GPMC
    Lenovo Ideapad S400 - Attractive Design And Affordable Price (Part 2)
    Top 10
    Roku Streaming Stick - Use Your Tv’s Remote To Control The Streaming Stick
    Simple Steps To Better Pictures (Part 2)
    Simple Steps To Better Pictures (Part 1)
    Technique Of The Month - Winter Wonders (Part 2)
    Technique Of The Month - Winter Wonders (Part 1)
    Share Movies Online With YouTube Capture For iOS
    The Gear We’re Lusting After…This Month
    Windows Phone 8 Group Test – June 2013 (Part 4)
    Windows Phone 8 Group Test – June 2013 (Part 3) : Nokia Lumia 820, Nokia Lumia 920
    Windows Phone 8 Group Test – June 2013 (Part 2) : Huawei Ascend W1, Nokia Lumia 620