2010-08-25 24 views
6

Tengo un objeto padre se llama página que tiene una lista de objetos denominada Control:Notificar Un objeto cuando una propiedad de otro objeto cambia

public class Page 
{ 
    List<CustomControl> controls {get;set;} 
} 

La clase CustomControl tiene la siguiente Defintion:

public class CustomControl 
{ 
string Name {get;set;} 
string Value {get;set;} 
} 

Digamos por ejemplo que la clase Page tiene dos controles personalizados A y B. ¿Es posible notificar el control personalizado B cuando la propiedad Valor del control personalizado A cambia para que pueda cambiar algunas de sus propiedades?

Estaba pensando en implementar el evento INotifyPropertyChanged en la clase CustomControl ahora, ¿cómo notifico una instancia de CustomControl cuando otra instancia de la misma clase tiene alguna propiedad de su Modificado?

+0

Aquí está el enlace de MSDN que habla de ello http://msdn.microsoft.com/en-us/library/ms743695.aspx – kalehv

Respuesta

5
public class CustomControl : INotifyPropertyChanged 
{ 
    private string _name; 
    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      if (_name == value) return; 
      _name = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Name")); 
     } 
    } 

    private string _value; 
    public string Value 
    { 
     get { return _value; } 
     set 
     { 
      if (_value == value) return; 

      _value = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Value")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 

    internal virtual void OnSiblingInPagePropertyChanged(object sender, PropertyChangedEventArgs args) 
    { 

    } 
} 

public class CustomControlObservableColletion : ObservableCollection<CustomControl> 
{ 
    // Required because, by default, it is not possible to find out which items 
    // have been cleared when the CollectionChanged event is fired after a .Clear() call. 
    protected override void ClearItems() 
    { 
     foreach (var item in Items.ToList()) 
      Remove(item); 
    } 

} 

public class Page 
{ 
    public IList<CustomControl> Controls { get; private set; } 

    public Page() 
    { 
     var controls = new CustomControlObservableColletion(); 
     controls.CollectionChanged += OnCollectionChanged; 
     Controls = controls; 
    } 

    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     switch (e.Action) 
     { 
      case NotifyCollectionChangedAction.Add: 
       RegisterControls(e.NewItems); 
       break; 

      case NotifyCollectionChangedAction.Replace: 
       RegisterControls(e.NewItems); 
       DeRegisterControls(e.OldItems); 
       break; 

      case NotifyCollectionChangedAction.Remove: 
       DeRegisterControls(e.OldItems); 
       break; 
     } 
    } 

    private void RegisterControls(IList controls) 
    { 
     foreach (CustomControl control in controls) 
      control.PropertyChanged += OnControlPropertyChanged; 
    } 

    private void DeRegisterControls(IList controls) 
    { 
     foreach (CustomControl control in controls) 
      control.PropertyChanged -= OnControlPropertyChanged; 
    } 

    private void OnControlPropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     foreach (var control in Controls.Where(c => c != sender)) 
      control.OnSiblingInPagePropertyChanged(sender, e); 
    } 
} 
+0

Gracias por la solución a probarlo y hacerle saber cómo fue – Manthan

+0

@Manthan: ¿Funcionó? – Ani

+0

Perdón por la demora. Funciona para mi Gracias – Manthan

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

namespace PropertyNotificatioDemo 

{ 

    class Program 

    { 

     static void Main(string[] args) 

     { 

      PropertyNotification notification = new PropertyNotification(); //Create an object of PropertyNotification class. 

      //Create EmployeeValueChange handler. 

      PropertyNotification.EmployeeValueChange += new PropertyNotification.EmployeeNameHandler(PropertyNotification_EmployeeValueChange); 



      //Display a message. 

      Console.Write("Enter Value : "); 

      //Read a value and initilize it in property. 

      notification.EmployeeName = Console.ReadLine(); 

     } 



     //Handler for property notification is created. 

     static void PropertyNotification_EmployeeValueChange(object sender, EventArgs e) 

     { 

      Console.WriteLine("Employee name is changed."+sender); 

     } 

    } 



    public class PropertyNotification 

    { 

     //Create a private variable which store value. 

     private static string _employeeName; 

     //Create a delegate of named EmployeeNamed=Handler 

     public delegate void EmployeeNameHandler(object sender, EventArgs e); 



     //Create a event variable of EmployeeNameHandler 

     public static event EmployeeNameHandler EmployeeValueChange; 



     //Create a static method named OnEmployeeNameChanged 

     public static void OnEmployeeNameChanged(EventArgs e) 

     { 

      if (EmployeeValueChange != null) 

       EmployeeValueChange(_employeeName, e); 

     } 



     //Create a property EmployeeName 

     public string EmployeeName 

     { 

      get 

      { 

       return _employeeName; 

      } 

      set 

      { 

       //Check if value of property is not same. 

       if (_employeeName != value) 

       { 

        OnEmployeeNameChanged(new EventArgs()); 

        _employeeName = value; 

       } 

      } 

     } 

    } 

} 
Cuestiones relacionadas