I really like XAML and Data binding. I always follow the "No Code Behind" approach. Data binding is a simple
way for applications to present and interact with data. Data binding allows the flow of data between UI and data objects on user interface. When a binding
is established with INotifyPropertyChanged and
the data model is changed, it reflects to update automatically to the UI elements.
With the release of Microsoft Visual
Studio 2012 and .NET 4.5, we have got a new attribute “CallerMemberName”.
With this new feature, you can
write INotifyPropertyChanged (Interface) code without having to worry about renaming properties
and string parameters.
In order to use
this attribute, we first need to add a using statement to System
.Runtime.CopilerServices
- using System.Runtime.CompilerServices;
Here is a typical implementation
of INotifyPropertyChanged “ViewModelBase.cs”
- public event PropertyChangedEventHandler Property Changed;
-
- protected void IPropertyChanged(string Propertname) {
- PropertyChangedEventHandler Handler = this.PropertyChanged;
- if (Handler != null)
- Handler(this, new PropertyChangedEventArgs(Propertname));
- }
Here, we have PropertyChanged event. This is an event that
is specified by the interface.
- public abstract class ViewModelBase:INotifyPropertyChanged
The INotifyPropertyChanged Interface is included in System.ComponentModel namespace.
- using System.ComponentModel;
Here is a typical implementation of [CallerMemberName] “ViewModelBase.cs”.
- protected bool SetProperty < T > (ref T Storage, T Value, [CallerMemberName] string Propertname = null) {
- if (EqualityComparer < T > .Default.Equals(Storage, Value)) return false;
- Storage = Value;
- IPropertyChanged(Propertname);
- return true;
- }
Here is an implementation of model class, and we called the IPropertyChanged event in the Setter of model properties
- public class Person: ViewModelBase {
- private string _FirstName;
-
- public string FirstName {
- get {
- return _FirstName;
- }
- set {
-
- SetProperty(ref this._FirstName, value);
- }
- }
- private string _LastName;
-
- public string LastName {
- get {
- return _LastName;
- }
- set {
-
- SetProperty(ref this._LastName, value);
-
- }
- }
- }
Here is an implementation of model class in ViewModel.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BooleanConverterExample.Model;
-
- namespace BooleanConverterExample.ViewModel {
- public class MainWindowViewModel {
-
- public MainWindowViewModel() {
- startup();
- }
-
- public void startup() {
- Model.Person P1 = new Model.Person();
- P1.FirstName = "Karthikeyan";
- P1.LastName = "Karuppusamy";
- }
- }
- }
So, our implementations is done. We will add some breakpoints
in ViewModelBase class to test PropertyChanged event.
Output
Here, you can see the difference between object old value
and new value.
[SetProperty
Calling]
[SetProperty
Calling]
[SetProperty Calling]
I hope you liked this blog. Happy Coding.