At work this morning, I needed to find a ColorPicker to use on our Windows Forms application. I searched for a bit and finally settled on one built using WPF.
Downloaded the control and with some tweaking I managed to have it working and it fit in great with the look and feel of our application. Then we hit a snag. I needed to somehow detect when a different color was picked, and wouldn't you know it the control didn't have the event for this use-case.
After some digging around I found the INotifyPropertyChanged interface and man was it good. It fit like a glove and long story short I created an event I could hook into easily whenever a color was picked. I now have a nice reusable Windows Forms userControl we can use anywhere on our application.
The reason for this backstory is that hopefully you can see the benefit of using this interface. I had no idea it existed but after today I'm glad I found it.
We're going to build a little example on a Console application showing it's functionality.
Create a new Console application and create a Person class with some properties for it:
public class Person
{
private string _name = string.Empty;
private string _lastName = string.Empty;
private string _address = string.Empty;
public string Name
{
get { return this._name; }
set
{
this._name = value;
}
}
public string LastName
{
get { return this._lastName; }
set
{
this._lastName = value;
}
}
public string Address
{
get { return this._address; }
set
{
this._address = value;
}
}
}
Sergio, what's with the funky formatting?! Patience, you'll see!
Now let's have the class implement the INotifyPropertyChanged interface:
public class Person : INotifyPropertyChanged
You'll have to reference the System.ComponentModel:
using System.ComponentModel;
Now implement the interface contract:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
Cool, now we have an event handler!
Let's wire this class up so when a property is changed in the class it'll let the handler know.
public string Name
{
get { return this._name; }
set
{
this._name = value;
NotifyPropertyChanged("Name");
}
}
public string LastName
{
get { return this._lastName; }
set
{
this._lastName = value;
NotifyPropertyChanged("LastName");
}
}
public string Address
{
get { return this._address; }
set
{
this._address = value;
NotifyPropertyChanged("Address");
}
}
So now our class is wired up correctly for usage. Let's head on to the Main method of Program.cs
class Program
{
static void Main(string[] args)
{
Person sergio = new Person();
sergio.PropertyChanged += new PropertyChangedEventHandler(sergio_PropertyChanged);
sergio.Name = "Sergio";
Console.ReadLine();
sergio.Name = "Serg";
Console.ReadLine();
}
static void sergio_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine("Something changed!");
Console.WriteLine(e.PropertyName);
}
}
That wraps it up, hopefully you realize the HUGE benefit this can bring to your code and how you handle events and unwilling objects.
If you see something can be improved please leave some feedback.





MultiQuote





|