Professional Documents
Culture Documents
Introduction
Windows Presentation Foundation (WPF) is ships part of .NET frame work and Develop
our GUI application 2D,3D,graphics animation and media content using in this WPF.
In advantage of WPF to less your coding in application.and Easier animation and special
effects,Inherent Scalability Inherent scalability
The developer has to manually write the Code update each UI Elements That Display
value stored in the object member .In WPF you can less your code greatly by binding
the object directly with the UI using XAML syntax.A developer typically creates a class
to represent an entity (table) in the database,where CRUD operations performed by
Calling methods on the objects.
I will Converting 3 different approaches for displaying data in the object onto the UI
using WPF
Create Table:
I am going use the same "StudentInformation" table.The Columns for the table are
RollNo(int,not null)
Fname(varchar(10),not null)
Lname(varchar(10),not null)
Clgname(varchar(50),not null)
mark(int,not null)
Exam(varchar(15),not null)
Properties:
Properties
Insert Record
Update Record
Load Record
Using a property , a programmer can get access to data members as through they are
public fields, The set and Get methods of property are Contained inside the property
declaration.
Syntax:
private string _myName;
// Declare a myName property of type string
public string myName
{
get
{ return _myName;}
set {
myName = _value; }
}
The get method used the keyword return to return the field's value to caller.The set
method use the keyword value to receive the value being passed in from the user.
A property can omit either a get clause or the set clause,A property that has get is
called a Read-only set is called write-only property.
Create the "Students" class for the table as follows,The Set and Get methods
of property are continued inside the property declaration
class Students
{
private int _Rollno;
public int Rollno// Declare a Rollno property of type int
{
get { return _Rollno;}
set { _Rollno = value;}
}
private string _FirstName;
public string FirstName// Declare a FirstName property of type string
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string_Lastname;
public string Lastname// Declare a LastName property of type string
{
get { return _Lastname; }
set { _Lastname = value;}
}
private string _Collegename;
public string Collegename// Declare a CollegeName property of type string
{
get { return _Collegename; }
set { _Collegename = value;}
}
private int _Marksobtaine;
public int Marksobtaine// Declare a Marksobtaine property of type int
{
get { return _Marksobtaine; }
set { _Marksobtaine = value;}
}
private string _Exam;
public string Exam// Declare a Exam property of type string
{
get { return _Exam; }
set { _Exam = value;}
}
}
Insert Record
new
//create
SqlConnection("[.]");
connection
objCon.Open(); //open Connection
objcmd = new SqlCommand("insert into StudentInformation
values('"+_Rollno+"','"+_FirstName+"','"+_Lastname+"','"+_Marksobtaine
objcmd.ExecuteNonQuery();
}
Load Record:
In our Source Coding Read data from records and binding the value from particular property name
public void Load()
{
SqlConnection objCon = new SqlConnection("[.]");
objCon.Open();
objcmd = new SqlCommand("Select * from
StudentInformation
where Rollno=@Rollno", objCon);
//select
command
objcmd.Parameters.Add(new SqlParameter("@Rollno", _Rollno));
SqlDataReader objRDR = objcmd.ExecuteReader(); //read data
from records(table)
if (objRDR.Read())
//Bind records in
property
name
{
_FirstName = objRDR["Fname"].ToString();
_Lastname = objRDR["Lname"].ToString();
_Colleagename = objRDR["Clgname"].ToString();
_Marksobtaine = Convert.ToInt32(objRDR["Mark"]);
_Exam = objRDR["Exam"].ToString();
}
else
{
MessageBox.Show("Record Not Found" );
}
}
Update Records:
on
update
query,
we
first
assign
field
to
Fname=@Fname,Lname=@Lname,Mark=@Mark,Clgname=@Clgname
I figured that since it is XAML that we are demonstrating, might as well develop the UI
in Microsoft Cider, the Visual Studio 2010 add-in visual designer for XAML. So, to use
the above class the coding for 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">
<Grid Height="285" Width="286">
<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>
Content="Load"
Content="Update"
</Grid>
</Window>
objStudent is object of student class now we call methods for inside load, insert
and update Button Click Event
like this
objStudent.Insert();
objStudent.Load();
objStudent.update();
After call objects we binding the Textbox value in particular Property name
In our Source Code
// insert Record
private void btnAdd_Click(object sender,RoutedEventArgs e)
{
objStudent.Rollno = Convert.ToInt32(txtRollNo.Text);
objStudent.FirstName = txtFirstName.Text;
objStudent.Lastname = txtLastName.Text;
objStudent.Colleagename = txtClgname.Text;
objStudent.Marksobtaine = Convert.ToInt32(txtMarks.Text);
objStudent.Exam = txtexamresult.Text;
objStudent.Insert();
//call object for insert record
MessageBox.Show("Record inserted", "Insert");
}
//load record
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
objStudent.Rollno =Convert.ToInt32(txtRollNo.Text);
objStudent.Load(); //call object for Load record
txtFirstName.Text =objStudent.FirstName;
txtLastName.Text =objStudent.Lastname;
txtClgname.Text =objStudent.Colleagename;
txtMarks.Text =Convert.ToString(objStudent.Marksobtaine);
txtexamresult.Text =objStudent.Exam;
}
//update record
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
objStudent.Rollno = Convert.ToInt32(txtRollNo.Text);
objStudent.FirstName = txtFirstName.Text;
objStudent.Lastname = txtLastName.Text;
objStudent.Colleagename = txtClgname.Text;
objStudent.Marksobtaine = Convert.ToInt32(txtMarks.Text);
objStudent.Exam = txtexamresult.Text;
objStudent.update();
//call object for update record
MessageBox.Show("Update Record", "Update");
}
This articles would be helpful for Beginners to learn WPF data binding.
Download
Download source code for WPF Data Binding Tutorial 2
Introduction
We already saw basics of WPF Binding in Chapter 1 (refer WPF Data Binding Tutorial 1)
Now we would see the next topic i.e. Using C#[INotifyPropertyChanged].In this
chapter, I would cover usage of INotifyPropertyChanged and how to bind data in WPF.
What is the purpose, we need to use INotifyPropertyChanged?
Event is
fired whenever a property Changed. In the following example we see how the UI can
know that this event is fired.
using System.ComponentModel;
using System.Data;
namespace WPF_Tutorial
{
public
class Students:INotifyPropertyChanged
{
#region Studentdetails
OnPropertyChanged("Rollno");
}
}
private string _FirstName;
public string FirstName
{
get{ return _FirstName; }
set{ _FirstName = value;
OnPropertyChanged("FirsName");
}
}
private string _Lastname;
public string Lastname
{
get{ return _Lastname; }
set{ _Lastname = value;
OnPropertyChanged("Lastname");
}
}
private string _Colleagename;
public string Colleagename
{
OnPropertyChanged("Colleagename");
}
}
private int _Marksobtaine;
public int Marksobtaine
{
get{ return _Marksobtaine; }
set{ _Marksobtaine = value;
OnPropertyChanged("Marksobtaine");
}
}
private string _Exam;
public string Exam
{
get{ return _Exam; }
set{ _Exam = value;
OnPropertyChanged("Exam");
}
}
#endregion
public Students()
{
}
#region insertDetail
public void Insetrt()
{
SqlConnectionobjCon = new SqlConnection("");//connection name
try
{
objCon.Open();
objcmd = new SqlCommand("insert into StudentInformation
values(@Rollno,@Fname,@Lname,@Mark,@clgname,@Exam) ", objCon);
objcmd.Parameters.AddWithValue("@Rollno", Rollno);
objcmd.Parameters.AddWithValue("@Fname", FirstName);
objcmd.Parameters.AddWithValue("@Lname", Lastname);
objcmd.Parameters.AddWithValue("@clgname", Colleagename);
objcmd.Parameters.AddWithValue("@Mark", Marksobtaine);
objcmd.Parameters.AddWithValue("@Exam", Exam);
//On property
changed
OnPropertyChanged("Rollno");
OnPropertyChanged("FirstName");
OnPropertyChanged("Lastname");
OnPropertyChanged("Colleagename");
OnPropertyChanged("Marksobtaine");
OnPropertyChanged("Exam");
objcmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
finally
{
objcmd.Dispose();
if(objCon.State == ConnectionState.Open)
{
objCon.Close();
objCon.Dispose();
}
}
}
#endregion
#region LoadDetails
public void Load()
{
SqlConnection objCon = new SqlConnection(""); //connection name
objCon.Open();
objcmd = new SqlCommand("Select * from StudentInformation where
Rollno=@Rollno", objCon);//select command
objcmd.Parameters.Add(new SqlParameter("@Rollno", _Rollno));
try
{
SqlDataReader objRDR = objcmd.ExecuteReader();
if(objRDR.Read())
{
_FirstName = objRDR["Fname"].ToString();
_Lastname = objRDR["Lname"].ToString();
_Colleagename =objRDR["Clgname"].ToString();
_Marksobtaine = Convert.ToInt32(objRDR["Mark"]);
_Exam = objRDR["Exam"].ToString();
//property changed
OnPropertyChanged("Rollno");
OnPropertyChanged("FirstName");
OnPropertyChanged("Lastname");
OnPropertyChanged("Colleagename");
OnPropertyChanged("Marksobtaine");
OnPropertyChanged("Exam");
}
else
{
MessageBox.Show("RecordNot Found");
}
}
catch(Exception ex)
{
MessageBox.Show("" + ex);
}
finally
{
objcmd.Dispose();
if(objCon.State == ConnectionState.Open)
{
objCon.Close();
objCon.Dispose();
}
}
}
#endregion
#region UpdateDetails
public void update()
{
OnPropertyChanged("Rollno");
OnPropertyChanged("FirstName");
OnPropertyChanged("Lastname");
OnPropertyChanged("Colleagename");
OnPropertyChanged("Marksobtaine");
OnPropertyChanged("Exam");
objcmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
finally
{
objcmd.Dispose();
if(objCon.State == ConnectionState.Open)
{
objCon.Close();
objCon.Dispose();
}
}
}
#region INotifyPropertyChanged Members
new
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.ComponentModel;
namespace WPF_Tutorial
{
public partial class Dataentry : Window
{
Students objStudent = new Students();
public Dataentry()
{
InitializeComponent();
}
//insert record
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(object sender, RoutedEventArgs e)
{
if (txtRollNo.Text != null)
{
try
{
objStudent.Rollno = Convert.ToInt32(txtRollNo.Text);
objStudent.Load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("enter correct code");
}
}
//update record
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
objStudent.PropertyChanged += objStudent_PropertyChanged;
}
private
void
objStudent_PropertyChanged(object
sender,
PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "Rollno":
txtRollNo.Text = Convert.ToString(objStudent.Rollno);
break;
case "FirstName":
txtFirstName.Text =objStudent.FirstName;
break;
case "Lastname":
txtLastName.Text = objStudent.Lastname;
break;
case "Colleagename":
txtClgname.Text = objStudent.Colleagename;
break;
case "Marksobtaine":
txtMarks.Text
=
Convert.ToString(objStudent.Marksobtaine);
break;
case "Exam":
txtexamresult.Text = objStudent.Exam;
break;
}
}
txtClgname.Text;
}
private void txtMarks_TextChanged(object sender, TextChangedEventArgs e)
{
objStudent.Marksobtaine = Convert.ToInt32(txtMarks.Text);
}
private void txtexamresult_TextChanged(object sender, TextChangedEventArgs
e)
{
objStudent.Exam = txtexamresult.Text;
}
}
}
Note also that the Insert "Load" and "Update" button event handlers do not
contain the Object-to-UI and UI-to-Object update code as that is now being
handled
by
the
event
handlers.
However, just because we implemented the INotifyPropertyChanged interface
in our Student class, we were not absolved of the responsibility to write custom event
handlers for the object and UI update logic. The whole point of all the above code is to
show the tedious coding required to keep the object and UI in sync.
Conclusion
In this articles, we learnt using INotifyPropertyChanged
and how property
Changed.Next Chapter, we will see Using C# and XAML [INotifyPropertyChanged and
XAML]
it's useful for beginners
Download
Download source code for WPF Data Binding Tutorial 3
WPF
Binding in
Chapter1
and C#[INotifyPropertyChanged] in
Chapter 2(refer:http://tempuri.org/tempuri.html ).
in
action
,the
Binding
syntax
in
the
textboxes
<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"
Loaded="Window_Loaded">
WindowStartupLocation="CenterScreen"
<TextBlock
Grid.Row="3"
Grid.ColumnSpan="2">CollegeName::</TextBlock>
Margin="22,10,185,12"
<TextBlock
Grid.ColumnSpan="2">Mark::</TextBlock>
Margin="62,10,185,13"
Grid.Row="4"
Margin="0,7,40,10"
Name="txtLastName"
<TextBox
Margin="0,7,40,10"
Grid.Row="2"
Grid.Column="1"
Margin="0,7,40,10"
Margin="0,7,40,10"
Text="{Binding Path=Marksobtaine}"></TextBox>
Name="txtexamresult"
<TextBox
Margin="0,7,40,10"
Grid.Row="5"
Grid.Column="1"
Name="btnAdd"
Content="Insert"
Margin="12,12,14,0"
<Button
Grid.Row="6"
Grid.Column="1"
Name="btnLoad"
Margin="13,12,102,0" Click="btnLoad_Click"></Button>
<Button Grid.Row="6" Grid.Column="1" Name="btnUpdate"
Margin="91,12,12,0" Click="btnUpdate_Click"></Button>
Content="Load"
Content="Update"
</Grid>
</Window>
The Binding Statement added to the XAML above allows you to discard all the eventhandlers 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);
}
}
}
{
myGrid.DataContext = objStudent;
}
}
}