Monday, January 2, 2012

INotify in C#.net



INotify in C#.net

To support OneWay or TwoWay binding to enable your binding target properties to automatically reflect the dynamic changes of the binding source (for example, to have the preview pane updated automatically when the user edits a form), your class needs to provide the proper property changed notifications. This example shows how to create a class that implements INotifyPropertyChanged.

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

Example:

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

namespace Business.ServiceMonitor
{
    public partial class TransactionItem : INotifyPropertyChanged
    {
        #region property calss

        private long _messageId;
        public long MessageID
        {
            get { return _messageId; }
            set
            {
                _messageId = value;
                OnPropertyChanged("MessageID");
            }
        }

        private string _messageBody;
        public string MessageBody
        {
            get { return _messageBody; }
            set
            {
                _messageBody = value;
                OnPropertyChanged("MessageBody");
            }
        }

        private DateTime _logDateTime;
        public DateTime LogDateTime
        {
            get { return _logDateTime; }
            set
            {
                _logDateTime = value;
                OnPropertyChanged("LogDateTime");
            }
        }

        private string _clientType;
        public string ClientType
        {
            get { return _clientType; }
            set
            {
                _clientType = value;
                OnPropertyChanged("ClientType");
            }
        }

        private string _status;
        public string Status
        {
            get { return _status; }
            set
            {
                _status = value;
                OnPropertyChanged("Status");
            }
        }

        private string _sessionId;
        public string SessionId
        {
            get { return _sessionId; }
            set
            {
                _sessionId = value;
                OnPropertyChanged("SessionId");
            }
        }

        private string _messageType;
        public string MessageType
        {
            get { return _messageType; }
            set
            {
                _messageType = value;
                OnPropertyChanged("MessageType");
            }
        }

        #endregion

        // Create the OnPropertyChanged method to raise the event
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

    }
}


No comments: