Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Bind Objects to Windows Presentation Foundation Controls
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Bind Data to Controls > How to: Bind Objects to Windows Presentation Foundation Controls

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

Telerik OpenAccess ORM enables you to bind Windows Presentation Foundation (WPF) elements such as a ListBox or ComboBox to IQueryable query result. You can bind directly to the query or to the executed result (for example to the product of ToList() method).

Telerik OpenAccess ORM Q1 2010 Release comes with several new or renamed assemblies:

  • Telerik.OpenAccess.Query.dll is renamed to Telerik.OpenAccess.35.Extensions.dll
  • Telerik.OpenAccess.40.dll is renamed to Telerik.OpenAcces.40.Extensions.dll
  • There is separation of the Win and Web UI into separate assemblies out of Telerik.OpenAccess.dll. These are Telerik.OpenAccess.Web.dll and Telerik.OpenAccess.Windows.dll assemblies.

The example in this topic is based on the Northwind persistent model. To run the code in this example, you must have already added the Northwind Model project to your project and configured your project to use Telerik OpenAccess ORM. To do this, complete the procedures in How to: Use a Data Model Defined in a Class Library.

The following example is from the code-behind page for an Extensible Application Markup Language (XAML) page that defines the SalesOrders window in WPF. When the window is loaded, a query of OrderOA and related OrderDetailOA objects is initialized and used as a data source. This result is bound to the DataContext property of a Grid control.

C# Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;
using OpenAccessNorthwindMapping;
namespace WpfApplication1
{
   
/// <summary>
   
/// Interaction logic for Window1.xaml
   
/// </summary>
   
public partial class SalesOrders : Window
   {
       
private IObjectScope context;
       
//private int customerId = 277;

       
public SalesOrders()
       {
           InitializeComponent();
       }
       
private void Window_Loaded(object sender, RoutedEventArgs e)
       {
           
try
           {
               
// Instantiate the ObjectContext.
               
context = ObjectScopeProvider1.GetNewObjectScope();
               
// Define a query that returns orders for a customer.
               
IQueryable<OrderOA> query = context.Extent<OrderOA>();
               
// You can execute the query and bind the result to the OrderItems control.
               
this.orderItemsGrid.DataContext = query; //.ToList<OrderOA>();
           }
           
catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
       
private void buttonClose_Click(object sender, RoutedEventArgs e)
       {
           
this.Close();
       }
   }
}
VB.NET Copy Code
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports Telerik.OpenAccess
Imports Telerik.OpenAccess.Query
Imports OpenAccessNorthwindMapping
Namespace WpfApplication1
 ''' <summary>
 ''' Interaction logic for Window1.xaml
 ''' </summary>
 Public Partial Class SalesOrders
  Inherits Window
  Private context As IObjectScope
  'private int customerId = 277;

  Public Sub New()
   InitializeComponent()
  End Sub
  Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
   Try
    ' Instantiate the ObjectContext.
    context = ObjectScopeProvider1.GetNewObjectScope()
    ' Define a query that returns orders for a customer.
    Dim query As IQueryable(Of OrderOA) = context.Extent(Of OrderOA)()
    ' You can execute the query and bind the result to the OrderItems control.
     '.ToList<OrderOA>();
    Me.orderItemsGrid.DataContext = query
   Catch ex As Exception
    MessageBox.Show(ex.Message)
   End Try
  End Sub
  Private Sub buttonClose_Click(sender As Object, e As RoutedEventArgs)
   Me.Close()
  End Sub
 End Class
End Namespace

The following is the XAML that defines the SalesOrders window in WPF. The ItemsSource property of a ComboBox is bound to the IQueryable<OrderOA> query data source that is defined in the code-behind page. When an order is selected, the related collection of OrderDetailOA objects is bound to the ListView that is specified by the ItemsSource property. A path value of Path=OrderDetailsOA in the binding ensures that the ListView is bound to the OrderDetailsOA property that returns an list of OrderDetailsOA.

XAML Copy Code
<Window x:Class="WpfApplication1.SalesOrders"
   
Title="Customer Sales Orders" Height="335" Width="425"
       
Name="SalesOrdersForm" Loaded="Window_Loaded">
   
<Grid Name="orderItemsGrid">
       
<ComboBox DisplayMemberPath="OrderID" ItemsSource="{Binding}"
                 
IsSynchronizedWithCurrentItem="true"
                 
Height="23" Margin="122,12,198,0" Name="comboBoxOrder" VerticalAlignment="Top"/>
       
<ListView ItemsSource="{Binding Path=OrderDetailsOA}" Name="listViewItems" Margin="34,46,34,50">
           
<ListView.View>
               
<GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Line Items">
                   
<GridViewColumn DisplayMemberBinding="{Binding Path=ProductID}"
                       
Header="Product" Width="50"/>
                   
<GridViewColumn DisplayMemberBinding="{Binding Path=Quantity}"
                       
Header="Quantity" Width="50"/>
                   
<GridViewColumn DisplayMemberBinding="{Binding Path=UnitPrice}"
                       
Header="Cost" Width="50"/>
                
</GridView>
           
</ListView.View>
       
</ListView>
       
<Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top"
              
HorizontalAlignment="Left" Width="93">Order:</Label>
       
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12"
               
Name="buttonClose" VerticalAlignment="Bottom" Width="75" Click="buttonClose_Click">Close</Button>
   
</Grid>
</
Window>