Thursday 7 November 2013

WPF - Dependency Object and Data Binding

In this article we are going to see the Dependency object and the Data binding in WPF.So first create a class name with Employee and derive it from Dependency object , then give five properties FirstName, LastName, Address, Domain, Version. Now give a static value and attach in stack panel resource and bind the data to the textblock present in the Stack Panel.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WpfApplication1
{
    class Employee:DependencyObject
    {

        public static DependencyProperty FirstNameProperty = DependencyProperty.Register("FirstName", typeof(string), typeof(Employee));
        public string FirstName
        {
            set { SetValue(FirstNameProperty, value); }
            get { return (string)GetValue(FirstNameProperty); }
        }

        public static DependencyProperty LastNameProperty = DependencyProperty.Register("LastName", typeof(string), typeof(Employee));
        public string LastName
        {
            set { SetValue(LastNameProperty, value); }
            get { return (string)GetValue(LastNameProperty); }
        }

        public static DependencyProperty AddressProperty = DependencyProperty.Register("Address", typeof(string), typeof(Employee));
        public string Address
        {
            set { SetValue(AddressProperty, value); }
            get { return (string)GetValue(AddressProperty); }
        }

        public static DependencyProperty DomainProperty = DependencyProperty.Register("Domain", typeof(string), typeof(Employee));
        public string Domain
        {
            set { SetValue(DomainProperty, value); }
            get { return (string)GetValue(DomainProperty); }
        }

        public static DependencyProperty VersionProperty = DependencyProperty.Register("Version", typeof(Int32), typeof(Employee));
        public int Version
        {
            set { SetValue(VersionProperty, value); }
            get { return (int)GetValue(VersionProperty); }
        }

    }
}



<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Dependency Object" Height="184" Width="300" Background="CornflowerBlue">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Name="Emp" Orientation="Vertical">
            <StackPanel.Resources>
                <local:Employee x:Key="empval" FirstName="Rajesh" LastName="G" Address="Porur" Domain="Dotnet" Version="4" />                                   
            </StackPanel.Resources>
            <TextBlock Foreground="Brown"  Text="{Binding FirstName}"/>
            <TextBlock Foreground="Brown" Text="{Binding LastName}"/>
            <TextBlock Foreground="Brown" Text="{Binding Address}"/>
            <TextBlock Foreground="Brown" Text="{Binding Domain}"/>
            <TextBlock Foreground="Brown" Text="{Binding Version}"/>
            <StackPanel.DataContext>
                <Binding Source="{StaticResource empval}" />
            </StackPanel.DataContext>
        </StackPanel>
    </Grid>
</Window>


Output :



From this article you can see the basics of Data binding and Dependency object.










No comments:

Post a Comment