2015-01-07 4 views
0

파생 클래스에서 기본 클래스 이벤트를 구독하는 데 어려움을 겪고 있습니다. 그 목적은 propertyChild를 얻을 때마다 내 기본 클래스에서 이벤트를 발생시켜 결과적으로 파생 클래스에서 메서드를 실행하는 것입니다.파생 클래스의 기본 클래스 이벤트 구독

namespace MyNamespace 
{ 
public delegate void EventHandler(); 
[Serializable] 
public class ChartGroupCollection : ObservableCollection<ChartGroup>, INotifyCollectionChanged 
{ 

    public ChartGroupCollection() 
    { 
     base.DirtyFlagging += new EventHandler(MethodIWantToRun); //subscription to event 
    } 

    void MethodIWantToRun() 
    { 
     SomeVariable++; 
    } 


    #region NotifyPropertyChanged 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion 
} 


[Serializable] 
public class ChartGroup : INotifyPropertyChanged 
{ 
    public event EventHandler<EventArgs> DirtyFlagging; 
    protected virtual void OnDirtyFlagging(EventArgs e) 
    { 
     EventHandler<EventArgs> handler = DirtyFlagging; 
     if (handler != null) 
     { 
      handler(this, e); 
     } 
    } 


    public ChartGroup() 
    { 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      DirtyFlagging.Invoke(this, someeventargs); //Where i want to invoke the event 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 


} 
+0

주제 끄기 : 여기 내 코드의 축소 버전입니다 그렇지 'ObservableCollection'이 이미 그것을 구현했기 때문에'ChartGroupCollection' 클래스에'INotifyCollectionChanged' 인터페이스를 구현할 필요가 있습니다; 편집 : 오타 – grabthefish

+0

기본 클래스와 파생 클래스라는 용어를 사용하는 것은 다소 복잡합니다. ChartGroupCollection의 기본 클래스는 ObservableCollection이고 차트 그룹은 – AxdorphCoder

답변

0

난 당신이 이벤트 트리거이

public class ChartGroupCollection : ObservableCollection<ChartGroup> 
{ 
    public int SomeVariable { get; set; } 

    public ChartGroupCollection() 
    { 
     CollectionChanged += ChartGroupCollection_CollectionChanged; 
    } 

    void ChartGroupCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
     if (e.OldItems != null) 
     { 
      foreach (ChartGroup item in e.OldItems) 
      { 
       item.PropertyChanged -= item_PropertyChanged; 
      } 
     } 

     if (e.NewItems != null) 
     { 
      foreach (ChartGroup item in e.NewItems) 
      { 
       item.PropertyChanged += item_PropertyChanged; 
      } 
     } 
    } 

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     MethodIWantToRun(); 
    } 

    private void MethodIWantToRun() 
    { 
     SomeVariable++; 
    } 
} 

public class ChartGroup : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    int _myProperty; 
    public int MyProperty 
    { 
     set { 
      _myProperty = value; 
      NotifyPropertyChanged(); 
     } 
    } 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

전화처럼 뭔가를하고 싶은 생각 :

ChartGroupCollection chartGroupCollection = new ChartGroupCollection(); 
var group = new ChartGroup(); 
chartGroupCollection.Add(group); 
group.MyProperty = 3; 
+0

이 아니므로 솔루션에 더 가까워집니다. 하지만 시작시 프로그램 충돌이 발생합니다. ChartGroupCollection_CollectionChanged의 "개체 참조가 개체의 인스턴스로 설정되어 있지 않다"고 주장합니다. 어떤 아이디어? 나는 그들에게서 벗어났다. – GSammons

+0

e.OldItems와 e.NewItems에 null 체크를 추가해야한다. 솔루션을 업데이트했습니다. – AxdorphCoder

+0

예. 저는 제 코드에 대해 혼자서 그렇게했습니다. 편집 할 때 1 분 정도 나를 때려. 이제는 깨끗하게 돌아갑니다. 그래도이 솔루션은 이벤트를 전혀 발생시키지 않습니다 – GSammons