views:

88

answers:

4

I am creating a custom control and i want to add some properties in it. On few of the properties i want to create some events. Say if i have a property

public int Date {get; set;}

now if its value is changing i want to trigger a change event. SO how can i add event on this

A: 

Well, you will need to define your event first of all, and a method to raise it.

Then you will need to switch away from an auto implemented property

private int _date;
public int Date
{
    get {return _date;}
    set
    {
        if(!_date.Equals(value))
            //Raise event here

        _date = value;
    }
}

If you need some help with the events part, here is a tutorial that I wrote to give you the detail.

Mitchel Sellers
+1  A: 

Use a "normal" property rather than an automatic property, and raise the change event in the setter:

private int _date;

public int Date
{
  get { return _date; }
  set
  {
    if (value != _date)
    {
      _date = value;
      // raise change event here
    }
  }
}

To raise the change event, if this is a standard INotifyPropertyChanged.PropertyChanged event:

PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
  handler(this, new PropertyChangedEventArgs("Date");
}

It's recommended practice to isolate this into an OnPropertyChanged method.

If you're raising a custom DateChanged event, the logic will be similar but with different names and event args.

itowlson
A: 

The typical pattern to do this would be like so:

// declare the event
public event EventHandler DateChanged;

// declare backing field for the property
private int _date;

public int Date
{
    get { return _date; }
    set
    {
        // bool indicating whether the new value is indeed 
        // different from the old one
        bool raiseEvent = _date != value;
        // assign the value to the backing field
        _date = value;
        // raise the event if the value has changed
        if (raiseEvent)
        {
            OnDateChanged(EventArgs.Empty);
        }
    }
}

protected virtual void OnDateChanged(EventArgs e)
{
    EventHandler temp = this.DateChanged;
    // make sure that there is an event handler attached
    if (temp != null)
    {
        temp(this, e);
    }
}

This example shows the implementation of an PropertyChanged event. For a PropertyChanging event, it's the same thing, but you raise the event before assigning the value in the property set accessor.

Fredrik Mörk
A: 

Also you can implement INotifyPropertyChanged interface and just raise an event in you property setter, here is full code sample that you can use and play with:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ConsoleApplication1
{
    class Foo : INotifyPropertyChanged
    {
        private object myProperty;
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(sender, e);
            }
        }

        public object MyProperty
        {
            get { return this.myProperty;}
            set
            {
                if (this.myProperty != value)
                {
                    this.myProperty = value;
                    this.OnPropertyChanged(this, new PropertyChangedEventArgs("MyPropery"));
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo foo = new Foo();
            foo.PropertyChanged += new PropertyChangedEventHandler(foo_PropertyChanged);

            foo.MyProperty = "test";
        }

        static void foo_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine("raised");
        }
    }
}
Restuta