WPF binding inotifypropertychanged example
In the previous wpf tutorial we learned about WPF Data Binding, different binding modes in WPF, One Way WPF DataBinding simple example. In this article we will learn about INPC data binding in WPF with an example c#.
Why use INotifyPropertyChanged with bindings in WPF?
It is possible that more than one person will be editing or adjusting the data that is displayed in our program. If the underlying data changes, we want our view to be updated. There we will use wpf inotifypropertychanged observablecollection.
INotifyPropertyChanged requires that we add a using statement for SystemComponentModel and it requires that we implement an event. The event we must implement is called PropertyChanged; it is of type PropertyChangedEventHandler. We're going to call PropertyChanged each time one of our properties is updated. To facilitate that, we're going to use a helper method called OnPropertyChanged.
OnPropertyChanged uses the CallerMemberName and that requires a using statement for System.Runtime.CompilerServices and what that does is it passes in the name of the property that calls this method. Unfortunately, we can no longer use automatic properties because we need to call the OnPropertyChanged method every time we call the setter. Thus, we will create a backing variable for each of the properties and in the setter, we will call OnPropertyChanged after assigning the passed in value to the underlying backing variable. Find the sample code to implement INotifyPropertyChanged event binding in WPF.
Inotifypropertychanged wpf example c#
Let's start with an example, find the source code below:-
databinding inotifypropertychanged |
MainWindow.xaml
<Window x:Class="DataBindingOneWay.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="inotifypropertychanged wpf simple
example"
Height="350" Width="525">
<Grid>
<StackPanel Name="Display">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name: "
/>
<TextBlock Margin="5,0,0,0"
Text="{Binding Name}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Designation:
" />
<TextBlock Margin="5,0,0,0"
Text="{Binding Designation}" />
</StackPanel>
<TextBlock></TextBlock>
<Button Content="Change" Width="70" HorizontalAlignment="Left"
Click="Button_Click" />
</StackPanel>
</Grid>
</Window>
Employee.cs
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using
System.Threading.Tasks;
namespace DataBindingOneWay
{
public class Employee : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged();
}
}
private string designation;
public string Designation
{
get { return designation; }
set
{
designation = value;
OnPropertyChanged();
}
}
public static Employee GetEmployee()
{
var emp = new Employee()
{
Name = "Ravi",
designation = "Developer"
};
return emp;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(
[CallerMemberName] string caller = "" )
{
if ( PropertyChanged != null )
{
PropertyChanged( this,
new PropertyChangedEventArgs( caller ) );
}
}
}
}
Main.Window.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 DataBindingOneWay
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Employee emp;
public MainWindow()
{
InitializeComponent();
emp = new Employee()
{
Name = "Manish",
Designation = "QA"
};
DataContext = emp;
}
private void Button_Click( object sender, RoutedEventArgs e )
{
emp.Name = "Rk";
emp.Designation = "Developer";
}
}
}
I hope you will enjoy the development tips while programming with C# in WPF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. Also If you like this article, don't forget to share this article with your friends and colleagues.
WPF binding inotifypropertychanged example
Reviewed by Ravi Kumar
on
7:16 PM
Rating:
No comments: