1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Notify Clients when Changes Happen

Discussion in 'C#' started by arunlalds, Mar 13, 2010.

  1. arunlalds

    arunlalds Banned

    Joined:
    Mar 12, 2010
    Messages:
    43
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    student
    Location:
    India
    Implement the INotifyPropertyChanged interface (located in
    Code:
    System.ComponentModel).
    using System.ComponentModel;
    ...
    class MyDataClass : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new
    PropertyChangedEventArgs(propertyName));
    }
    }
    private int _tag = 0;
    public int Tag
    {
    get
    { return _tag; }
    set
    {
    _tag = value;
    OnPropertyChanged(“Tag”);
    }
    }
    }
    
    The Windows Presentation Foundation (WPF) makes extensive use of this interface for
    data binding, but you can use it for your own purposes as well.
    To consume such a class, use code similar to this:
    Code:
    void WatchObject(object obj)
    {
    INotifyPropertyChanged watchableObj = obj as INotifyPropertyChanged;
    if (watchableObj != null)
    {
    watchableObj.PropertyChanged += new
    PropertyChangedEventHandler(data_PropertyChanged);
    }
    }
    void data_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
    //do something when data changes
    }
    
     

Share This Page