ObjectDataProvider in WPF

ObjectDataProvider :

WPF provides us many features for developing Data Driven applications. One among those is ObjectDataProvider. It allows us to creates an instance of the class and makes calls to various methods also allowing to give values to its parameters. We will move towards a simple example.

 

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObjectDataProvider
{
    class DataAccess 
    {
        ObservableCollection<Employee> _EmpCollection;
        internal ObservableCollection<Employee> EmpCollection
        {
            get { return _EmpCollection; }
            set { _EmpCollection = value; }
        }

        public DataAccess()
        {
            _EmpCollection = new ObservableCollection<Employee>();
        }

        public ObservableCollection<Employee> GetEmp()
        {
            EmpCollection.Add(new Employee()
            {
                Name = "Aditya",
                Age = 23,
                gender = Employee.GenderType.Male
               
            }); 

            return EmpCollection;
        }
        
    }

    class Employee
    {

       public enum GenderType
        {
            Male = 1,
            female = 2
        }


        public string Name { get; set; }
        public long Age { get; set; }
        public GenderType gender { get; set; }
    }
}

Now we will move towards the main XAML file.

<Window x:Class="ObjectDataProvider.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:data="clr-namespace:ObjectDataProvider"
        Title="ObjectDataProviderDemo" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider x:Key="obj"
                             ObjectType="{x:Type data:DataAccess}"
                             MethodName="GetEmp">
            <!--Here you can write your parameters for your Method-->
            <ObjectDataProvider.MethodParameters>
                <!--<system:Double>0</system:Double>
                <local:TempType>Celsius</local:TempType>
                <x:Type TypeName="VerticalAlignment" />-->
            </ObjectDataProvider.MethodParameters>

        </ObjectDataProvider>
        <DataTemplate x:Key="EmpDataTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" ></TextBlock>   
                <TextBlock Text=" "/>
                <TextBox Text="{Binding Age}" />
            </StackPanel>
        </DataTemplate>

    </Window.Resources>

    <Grid DataContext="{Binding Source={StaticResource obj}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0"  ItemsSource="{Binding}" FontSize="30" ItemTemplate="{StaticResource EmpDataTemplate}"/>
        <!--<TextBox Grid.Row="1" Height="60" Text="{Binding Age}" FontSize="30"/>-->
        <TextBox Grid.Row="2" Height="60" Text="{Binding gender}" FontSize="30"/>
    </Grid>
</Window>

Here in the above example I have given an example of how to call a method without any parameters . For with parameters follow the commented code. Also I have given an example of how to use DataTemplate . Hope you have enjoyed.

Leave a comment