This sample show how we can use INotifyPropertyChanged Interface in WPF application.
Step(1) Create xaml file
<Window x:Class="WPFTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Text="{Binding Path=StudentFirstName}" VerticalAlignment="Top" Height="18" Margin="99,83,57,0" />
<TextBox Text="{Binding Path=StudentGradePointAverage}" Margin="99,113,57,127" />
</Grid>
</Window>
INotifyPropertyChanged
is an interface used in the data object classes to provide PropertyChanged
notification to clients when any property value gets changed.This allow us to raise PropertyChanged
event whenever the state of the object changes (Added, Removed, and Modified).Step(1) Create xaml file
<Window x:Class="WPFTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Text="{Binding Path=StudentFirstName}" VerticalAlignment="Top" Height="18" Margin="99,83,57,0" />
<TextBox Text="{Binding Path=StudentGradePointAverage}" Margin="99,113,57,127" />
</Grid>
</Window>
Step(2) Code behind file:
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.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
StudentData sd = new StudentData();
public Window1()
{
InitializeComponent();
this.DataContext = sd;
}
}
}
Step(3) Create data class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WPFTest
{
public class StudentData : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
string _firstName = "Shekhar";
public string StudentFirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
OnPropertyChanged("StudentFirstName");
}
}
double _gradePointAverage=10;
public double StudentGradePointAverage
{
get
{
return _gradePointAverage;
}
set
{
_gradePointAverage = value;
OnPropertyChanged("StudentGradePointAverage");
}
}
}
}
=============================================================
No comments:
Post a Comment