WPF Data Binding Tutorial 3

Gow.net
Posted by in WPF category on for Beginner level | Points: 250 | Views : 11561 red flag

it's useful for beginners


 Download source code for WPF Data Binding Tutorial 3

WPF Data Binding Tutorial 3:

  

Introduction

We already saw Basic of WPF Binding in Chapter1 (refer:http://tempuri.org/tempuri.html) and C#[INotifyPropertyChanged] in Chapter 2(refer:http://tempuri.org/tempuri.html ).
  
Now we will explore Using C# and XAML[INotifyPropertyChanged]. In this article ,the Data Binding Syntax in XAML is used and would show you how easy it is,to bind UI elements directly to an object that implements INotifyPropertyChanged.
To see WPF Data Binding in action ,the Binding syntax in the textboxes "Text" property values.

I have highlighted some codes in below example that"s needed to be added to implement INotifyPropertyChanged.We already know the INotifyPropertyChanged  implemented in Chapter 2(refer:http://tempuri.org/tempuri.html ).Now we  see XAML  page how to binding use Binding Property in Text below code i  highlighted some codes in below XAML


<Window x:Class="WPF_Tutorial.Dataentry"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Dataentry" Height="443" Width="371"

 Background="WhiteSmoke" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">

    <Grid Height="285" Width="286" x:Name="myGrid">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="99*"></ColumnDefinition>

            <ColumnDefinition Width="187*"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

        </Grid.RowDefinitions>

        <TextBlock Margin="54,20,0,0">Rollno::</TextBlock>

        <TextBlock Grid.Row="1" Margin="39,12,0,12">FirstName::</TextBlock>

        <TextBlock Grid.Row="2" Margin="34,9,0,14">LastName::</TextBlock>

        <TextBlock Grid.Row="3" Margin="22,10,185,12" Grid.ColumnSpan="2">CollegeName::</TextBlock>

        <TextBlock Grid.Row="4" Margin="62,10,185,13" Grid.ColumnSpan="2">Mark::</TextBlock>

        <TextBlock Grid.Row="5" Margin="33,10,3,17">ExamResult::</TextBlock>

        <TextBox Grid.Column="1" Name="txtRollNo"  Margin="0,7,40,10" 

Text="{Binding Path=Rollno}" ></TextBox>

        <TextBox Grid.Row="1" Grid.Column="1" Name="txtFirstName" Text="{Binding Path=FirstName}"  Margin="0,7,40,10" ></TextBox>

        <TextBox Grid.Row="2" Grid.Column="1" Name="txtLastName"  Margin="0,7,40,10" 

Text="{Binding Path=Lastname}" ></TextBox>

        <TextBox Grid.Row="3" Grid.Column="1" Name="txtClgname"  Margin="0,7,40,10"

Text="{Binding Path=Colleagename}" ></TextBox>

        <TextBox Grid.Row="4" Grid.Column="1" Name="txtMarks"  Margin="0,7,40,10" 

Text="{Binding Path=Marksobtaine}"></TextBox>

        <TextBox Grid.Row="5" Grid.Column="1" Name="txtexamresult"  Margin="0,7,40,10" 

Text="{Binding Path=Exam}" ></TextBox>

        <Button Grid.Row="6" Name="btnAdd" Content="Insert" Margin="12,12,14,0" Click="btnAdd_Click"></Button>

        <Button Grid.Row="6" Grid.Column="1" Name="btnLoad" Content="Load" Margin="13,12,102,0" Click="btnLoad_Click"></Button>

        <Button Grid.Row="6" Grid.Column="1" Name="btnUpdate" Content="Update" Margin="91,12,12,0" Click="btnUpdate_Click"></Button>

    </Grid>

</Window>

The Binding Statement added to the XAML above allows you to discard all the event-handlers in the code-behind that were need before.The function that we wanted from our app is now achievable with a lot less code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Shapes;

using System.ComponentModel;

namespace WPF_Tutorial

{

   public partial class Dataentry : Window

    {

        Students objStudent = new Students(); //create object for student class

        publicDataentry()

        {

            InitializeComponent();

        }

        //insert record

        private void btnAdd_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                objStudent.Insetrt();

                MessageBox.Show("Record inserted", "Insert");

                //clearField

txtRollNo.Text = "";

                txtFirstName.Text = "";

                txtLastName.Text = "";

                txtClgname.Text = "";

                txtexamresult.Text = "";

                txtMarks.Text = "";

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

        //load record

        private void btnLoad_Click(objectsender, RoutedEventArgs e)

        {

            if(txtRollNo.Text != null)

            {

                try

                {

           objStudent.Load();              

                }

                catch(Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

            }

            else

            {

                MessageBox.Show("enter correct code");

            }

        }

        //updaterecord

        private void btnUpdate_Click(objectsender, RoutedEventArgs e)

        {

            if(txtRollNo.Text != null)

            {

                try

                {

              

                    objStudent.update();

                    MessageBox.Show("Update Record", "Update");

                }

                catch(Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

            }

        }


        private void Window_Loaded(objectsender, RoutedEventArgs e)

        {

            myGrid.DataContext = objStudent;


        }

    }

}



Conclusion

This article is meant for beginners.



Page copy protected against web site content infringement by Copyscape

About the Author

Gow.net
Full Name: Gowthaman vimal
Member Level:
Member Status: Member
Member Since: 12/13/2011 12:55:26 PM
Country: India
gowthaman8870226416
http://www.dotnetfunda.com
DOTNET Developer

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)