Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Adding References and Defining User Interface
See Also
Programmer's Guide > Quick-Start Scenarios > WPF > Adding References and Defining User Interface

Glossary Item Box

In this task, you will use the already created empty WPF application in the Getting Started task. A reference to the SofiaCarRentalLibrary will be added, and the user interface for the main page will be prepared.

 

To add a reference to the SofiaCarRentalLibrary project:

  1. Right-click on the WpfOpenAccessIntegration project, then click AddReference.
  2. In the AddReference dialog, navigate to the Projects tab page, select SofiaCarRentalLibrary and press OK.

Defining User Interface

To define WPF client application user interface:

  1. In Solution Explorer, right-click and select Add Reference. This displays the Add Reference dialog box. Add a reference to the following Telerik OpenAccess ORM assemblies:
    1. Telerik.OpenAccess.dll
    2. Telerik.OpenAccess.35.Extensions.dll
  2. In Solution Explorer, right-click and select Add Reference. This displays the Add Reference dialog box. Add a reference to the following Telerik RadControls for WPF assemblies:
    1. Telerik.Windows.Controls.dll
    2. Telerik.Windows.Controls.Data.dll 
    3. Telerik.Windows.Data.dll
    4. Telerik.Windows.Controls.GridView.dll
    5. Telerik.Windows.Controls.Input.dll
  3. In Solution Explorer, double-click the MainPage.xaml file. This opens XAML markup for the MainPage class that is the user interface for the WPF application.
  4. Replace the existing XAML markup with the following markup that defines the application user-interface:
    XAML Copy Code
    <Window x:Class="WpfOpenAccessIntegration.MainWindow"
           
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Title="SofiaCarRental"
           
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
           
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
           
    xmlns:example="clr-namespace:WpfOpenAccessIntegration">
       
    <Window.Resources>
           
    <example:CarsViewModel x:Key="DataSource" />
       
    </Window.Resources>
       
    <Grid x:Name="LayoutRoot"
             
    DataContext="{Binding Source={StaticResource DataSource}}">
           
    <Grid.RowDefinitions>
               
    <RowDefinition Height="Auto" />
               
    <RowDefinition />
               
    <RowDefinition Height="Auto" />
           
    </Grid.RowDefinitions>
           
    <Grid Margin="13,13,0,0">
               
    <Grid.ColumnDefinitions>
                   
    <ColumnDefinition Width="Auto" />
                   
    <ColumnDefinition Width="Auto" />
                   
    <ColumnDefinition Width="Auto" />
                   
    <ColumnDefinition />
               
    </Grid.ColumnDefinitions>
               
    <TextBlock Text="Make: "
                          
    FontSize="16"
                          
    Margin="7" />
               
    <TextBox Text="{Binding Path=Make, Mode=TwoWay}"
                        
    Margin="7"
                        
    Grid.Column="1"
                        
    Width="100" />
               
    <telerik:RadButton Content="LoadCars"
                                  
    Margin="7"
                                  
    Grid.Column="2"
                                  
    Command="{Binding Path=LoadCarsCommand}" />
           
    </Grid>
           
    <telerikGrid:RadGridView Grid.Row="1"
                                    
    Margin="20,0,20,0"
                                    
    ItemsSource="{Binding Cars}"
                                    
    SelectedItem="{Binding SelectedCar, Mode=TwoWay}" />
           
    <Grid Grid.Row="2"
                 
    Margin="13,0,13,13">
               
    <Grid.ColumnDefinitions>
                   
    <ColumnDefinition />
                   
    <ColumnDefinition Width="Auto" />
                   
    <ColumnDefinition Width="Auto" />
                   
    <ColumnDefinition Width="Auto" />
               
    </Grid.ColumnDefinitions>
               
    <telerik:RadButton Content="Save"
                                  
    Margin="7"
                                  
    Grid.Column="1"
                                  
    Command="{Binding SaveChangesCommand}" />
               
    <telerik:RadButton Content="Add"
                                  
    Margin="7"
                                  
    Grid.Column="2"
                                  
    Command="{Binding AddCarCommand}" />
               
    <telerik:RadButton Content="Delete"
                                  
    Margin="7"
                                  
    Grid.Column="3"
                                  
    Command="{Binding DeleteCarCommand}" />
           
    </Grid>
       
    </Grid>
    </
    Window>


  5. Create a new class named DelegateCommand, implementing the ICommand interface.
    C# Copy Code
    using System;
    namespace WpfOpenAccessIntegration
    {
       
    public class DelegateCommand : System.Windows.Input.ICommand
       {
           
    private Predicate<object> _canExecute;
           
    private Action<object> _method;
           
    public event EventHandler CanExecuteChanged;
           
    public DelegateCommand(Action<object> method)
               :
    this(method, null)
           {
           }
           
    public DelegateCommand(Action<object> method, Predicate<object> canExecute)
           {
               _method = method;
               _canExecute = canExecute;
           }
           
    public bool CanExecute(object parameter)
           {
               
    if (_canExecute == null)
               {
                   
    return true;
               }
               
    return _canExecute(parameter);
           }
           
    public void Execute(object parameter)
           {
               _method.Invoke(parameter);
           }
           
    protected virtual void OnCanExecuteChanged(EventArgs e)
           {
               var canExecuteChanged = CanExecuteChanged;
               
    if (canExecuteChanged != null)
                   canExecuteChanged(
    this, e);
           }
           
    public void RaiseCanExecuteChanged()
           {
               OnCanExecuteChanged(EventArgs.Empty);
           }
       }
    }
    VB.NET Copy Code
    Public Class DelegateCommand
        Implements System.Windows.Input.ICommand
        Private _canExecute As Predicate(Of Object)
        Private _method As Action(Of Object)
        Public Event CanExecuteChanged As EventHandler Implements System.Windows.Input.ICommand.CanExecuteChanged
        Public Sub New(ByVal method As Action(Of Object))
            Me.New(method, Nothing)
        End Sub
        Public Sub New(ByVal method As Action(Of Object), ByVal canExecute As Predicate(Of Object))
            _method = method
            _canExecute = canExecute
        End Sub
        Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
            If _canExecute Is Nothing Then
                Return True
            End If
            Return _canExecute(parameter)
        End Function
        Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
            _method.Invoke(parameter)
        End Sub
        Protected Overridable Sub OnCanExecuteChanged(ByVal e As EventArgs)
            RaiseEvent CanExecuteChanged(Me, e)
        End Sub
        Public Sub RaiseCanExecuteChanged()
            OnCanExecuteChanged(EventArgs.Empty)
        End Sub
    End Class
  6. Add a new interface, named ICarsModel to the project.
    C# Copy Code
    using System.Collections.Generic;
    using SofiaCarRentalLibrary;
    namespace WpfOpenAccessIntegration
    {
       
    public interface ICarsModel
       {
           IEnumerable<Car> LoadCarsByMake(
    string make);
           
    void AddCar(Car car);
           
    void DeleteCar(Car car);
           
    void SaveChanges();
       }
    }
    VB.NET Copy Code
    Imports System.Collections.Generic
    Imports SofiaCarRentalLibrary
    Namespace WpfOpenAccessIntegration
     Public Interface ICarsModel
      Function LoadCarsByMake(ByVal make As String) As IEnumerable(Of Car)
      Sub AddCar(ByVal car As Car)
      Sub DeleteCar(ByVal car As Car)
      Sub SaveChanges()
     End Interface
    End Namespace
  7. Create a new class named CarsModel, implementing the ICarsModel interface.
    C# Copy Code
    using System.Collections.Generic;
    using SofiaCarRentalLibrary;
    using System.Linq;
    namespace WpfOpenAccessIntegration
    {
       
    public class CarsModel : ICarsModel
       {
           
    private SofiaCarRentalDbContext dbContext = new SofiaCarRentalDbContext();
           
    public IEnumerable<Car> LoadCarsByMake(string make)
           {
               
    return this.dbContext.Cars.Where(c => c.Make == make).ToList();
           }
           
    public void AddCar(Car car)
           {
           }
           
    public void DeleteCar(Car car)
           {
           }
           
    public void SaveChanges()
           {
           }
       }
    }
    VB.NET Copy Code
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports SofiaCarRentalLibrary

    Public Class CarsModel
        Implements ICarsModel
        Private dbContext As New SofiaCarRentalDbContext()

        Public Function LoadCarsByMake(ByVal make As String) As IEnumerable(Of SofiaCarRentalLibrary.Car) Implements ICarsModel.LoadCarsByMake
            Return Me.dbContext.Cars.Where(Function(c) c.Make = make).ToList()
        End Function

        Public Sub AddCar(ByVal car As SofiaCarRentalLibrary.Car) Implements ICarsModel.AddCar
            Throw New NotImplementedException()
        End Sub

        Public Sub DeleteCar(ByVal car As SofiaCarRentalLibrary.Car) Implements ICarsModel.DeleteCar
            Throw New NotImplementedException()
        End Sub

        Public Sub SaveChanges() Implements ICarsModel.SaveChanges
            Throw New NotImplementedException()
        End Sub
    End Class
  8. Create a new class named CarsViewModel. The CarsViewModel implements INotifyPropertyChanged. This will be the data source for the main page.
    C# Copy Code
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using SofiaCarRentalLibrary;

    namespace WpfOpenAccessIntegration
    {
       
    public class CarsViewModel : INotifyPropertyChanged
       {
           
    private ICarsModel dataModel;
           
    private Car selectedCar;
           
    private string make;
           
    private DelegateCommand loadCarsCommand;
           
    private DelegateCommand addCarCommand;
           
    private DelegateCommand deleteCarCommand;
           
    private DelegateCommand saveChangesCommand;
           
    public event PropertyChangedEventHandler PropertyChanged;

           
    public CarsViewModel()
           {
               
    this.dataModel = new CarsModel();
               
    this.Cars = new ObservableCollection<Car>();
               
    this.Make = "VW";
           }

           
    public ObservableCollection<Car> Cars
           {
               get;
               set;
           }

           
    public Car SelectedCar
           {
               get
               {
                   
    return selectedCar;
               }
               set
               {
                   
    if (this.selectedCar == value)
                       
    return;
                   
    this.selectedCar = value;
                   
    this.OnPropertyChanged("SelectedCar");
               }
           }

           
    public string Make
           {
               get
               {
                   
    return this.make;
               }
               set
               {
                   
    if (this.make == value)
                       
    return;
                   
    this.make = value;
                   
    this.OnPropertyChanged("Make");
               }
           }

           
    public DelegateCommand LoadCarsCommand
           {
               get
               {
                   
    if (this.loadCarsCommand == null)
                       
    this.loadCarsCommand = new DelegateCommand(this.OnLoadCarsCommandExecute);
                   
    return loadCarsCommand;
               }
           }

           
    public DelegateCommand AddCarCommand
           {
               get
               {
                   
    if (this.addCarCommand == null)
                       
    this.addCarCommand = new DelegateCommand(this.OnAddCommandExecute);
                   
    return addCarCommand;
               }
           }

           
    public DelegateCommand DeleteCarCommand
           {
               get
               {
                   
    if (this.deleteCarCommand == null)
                       
    this.deleteCarCommand = new DelegateCommand(this.OnDeleteCommandExecute);
                   
    return deleteCarCommand;
               }
           }

           
    public DelegateCommand SaveChangesCommand
           {
               get
               {
                   
    if (this.saveChangesCommand == null)
                       
    this.saveChangesCommand = new DelegateCommand(this.OnSaveCommandExecute);
                   
    return saveChangesCommand;
               }
           }

           
    private void OnLoadCarsCommandExecute(object parameter)
           {
               
    this.Cars.Clear();
               
    foreach (var item in this.dataModel.LoadCarsByMake(this.Make))
               {
                   
    this.Cars.Add(item);
               }
           }

           
    private void OnAddCommandExecute(object parameter)
           {
               Car newCar =
    new Car();
               newCar.Make =
    this.Make;
               newCar.Model =
    "DemoModel";
               
    this.Cars.Add(newCar);
               
    this.SelectedCar = newCar;
               
    this.dataModel.AddCar(newCar);
           }

           
    private void OnDeleteCommandExecute(object parameter)
           {
               
    if (this.SelectedCar != null)
               {
                   
    this.dataModel.DeleteCar(this.SelectedCar);
                   
    this.Cars.Remove(this.SelectedCar);
               }
           }

           
    private void OnSaveCommandExecute(object parameter)
           {
               
    this.dataModel.SaveChanges();
           }

           
    protected virtual void OnPropertyChanged(string info)
           {
               
    if (this.PropertyChanged != null)
                   
    this.PropertyChanged(this, new PropertyChangedEventArgs(info));
           }
       }
    }
    VB.NET Copy Code
    Imports System.ComponentModel
    Imports System.Collections.ObjectModel
    Imports SofiaCarRentalLibrary

    Public Class CarsViewModel
        Implements INotifyPropertyChanged
        Private dataModel As ICarsModel
        Private selectCar As Car
        Private carMake As String
        Private loadCarsCmd As DelegateCommand
        Private addCarCmd As DelegateCommand
        Private deleteCarCmd As DelegateCommand
        Private saveChangesCmd As DelegateCommand
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        Public Sub New()
            Me.dataModel = New CarsModel()
            Me.Cars = New ObservableCollection(Of Car)()
            Me.Make = "VW"
        End Sub

        Public Property Cars() As ObservableCollection(Of Car)
        Public Property SelectedCar() As Car
            Get
                Return selectCar
            End Get
            Set(ByVal value As Car)
                If Me.selectCar Is value Then
                    Return
                End If
                Me.selectCar = value
                Me.OnPropertyChanged("SelectedCar")
            End Set
        End Property

        Public Property Make() As String
            Get
                Return Me.carMake
            End Get
            Set(ByVal value As String)
                If Me.carMake = value Then
                    Return
                End If
                Me.carMake = value
                Me.OnPropertyChanged("Make")
            End Set
        End Property

        Public ReadOnly Property LoadCarsCommand() As DelegateCommand
            Get
                If Me.loadCarsCmd Is Nothing Then
                    Me.loadCarsCmd = New DelegateCommand(AddressOf Me.OnLoadCarsCommandExecute)
                End If
                Return loadCarsCmd
            End Get
        End Property

        Public ReadOnly Property AddCarCommand() As DelegateCommand
            Get
                If Me.addCarCmd Is Nothing Then
                    Me.addCarCmd = New DelegateCommand(AddressOf Me.OnAddCommandExecute)
                End If
                Return addCarCmd
            End Get
        End Property

        Public ReadOnly Property DeleteCarCommand() As DelegateCommand
            Get
                If Me.deleteCarCmd Is Nothing Then
                    Me.deleteCarCmd = New DelegateCommand(AddressOf Me.OnDeleteCommandExecute)
                End If
                Return deleteCarCmd
            End Get
        End Property

        Public ReadOnly Property SaveChangesCommand() As DelegateCommand
            Get
                If Me.saveChangesCmd Is Nothing Then
                    Me.saveChangesCmd = New DelegateCommand(AddressOf Me.OnSaveCommandExecute)
                End If
                Return saveChangesCmd
            End Get
        End Property

        Private Sub OnLoadCarsCommandExecute(ByVal parameter As Object)
            Me.Cars.Clear()
            For Each item In Me.dataModel.LoadCarsByMake(Me.Make)
                Me.Cars.Add(item)
            Next item
        End Sub

        Private Sub OnAddCommandExecute(ByVal parameter As Object)
            Dim newCar As New Car()
            newCar.Make = Me.Make
            newCar.Model = "DemoModel"
            Me.Cars.Add(newCar)
            Me.SelectedCar = newCar
            Me.dataModel.AddCar(newCar)
        End Sub

        Private Sub OnDeleteCommandExecute(ByVal parameter As Object)
            If Me.SelectedCar IsNot Nothing Then
                Me.dataModel.DeleteCar(Me.SelectedCar)
                Me.Cars.Remove(Me.SelectedCar)
            End If
        End Sub

        Private Sub OnSaveCommandExecute(ByVal parameter As Object)
            Me.dataModel.SaveChanges()
        End Sub

        Protected Overridable Sub OnPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        End Sub
    End Class

In this task, you have successfully created a sample WPF client application. Next, you will add a query to the CarsModel class that loads specific Car objects, and those Car objects will be bound to the grid control.

See Also