2013-08-26 1 views
1

이 사이트에서 이와 비슷한 질문을했지만 실제로 답변을 찾지 못했지만 다시 게시합니다.계산 된 속성의 변경을 알리기

public MyClass 
{ 

    public person Person1 {get; set;} 

    public person Person2 {get; set;} 

    CalculatedProperty 
    { 
    get 
    { return Person1.Salary + (Person2.Salary/2) ; } 
    } 

} 

public class Person 
{ 
    public double Salary 
    { get; set;} 
} 

나는 CalculatedProperty 때마다 Person1.Salary 및/또는 Person2.Salary 변화에 변화를 알릴 수 있어야합니다.

Person1Person2의 설정자에 OnPropertyChanged("CalculatedProperty")을 추가하려고했지만 작동하지 않으며 그 이유를 이해하지만 변경 사항을 알리는 방법을 찾을 수 없습니다. 당신은 두 가지 속성에 등록 후, 너무 INotifyPropertyChanged을 구현하기 위해 Person 필요

+0

알림으로 무엇을 의미합니까? – Renan

답변

5

) ...? ... 너무 노력

(이에 대한 ObjectDataProvider을 사용하는 방법이 있나요) = 도와주세요 당신 있다. PropertyChanged 그들 중 하나에 Salary 호출되면, 그냥 사람이 변경되는 경우 등록을 취소 기억 CalculatedProperty

void PersonPropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    if(e.PropertyName == "Salary") 
     OnPropertyChanged("CalculatedProperty"); 
} 

PropertyChanged를 호출합니다.

UPDATE : 짐, 당신의 세터 이런 식으로 뭔가 보일 것 말했듯이

:

private Person _person; 
public Person Person 
{ 
    get { return _person; } 
    set 
    { 
     if (Equals(value, _person)) return; 

     if (_person != null) 
      _person.PropertyChanged -= PersonPropertyChanged; 

     _person = value; 

     if(_person != null) 
      _person.PropertyChanged += PersonPropertyChanged; 

     OnPropertyChanged("Person"); 
    } 
} 
+0

급여와 CalculatedProperty가 같은 클래스에 없습니다. 이걸 시험해 봤어? – Paparazzi

+0

'MyClass'가'Person' 클래스의 OnChange 속성에 가입하도록합니다. –

+0

@Blam, 나도 알아, 그게 전부 핵심이야. 'Person'의'PropertyChanged' _at_'MyClass'에 등록하고 변경 사항을 청취합니다. 어느 쪽이든 Salary가 변경되면 CalculatedProperty에 대해 PropertyChanged를 호출합니다. – CKII

3

가지 적어 CLII 검사를하지만, 이것은 완전한 예를

using System.ComponentModel; 

namespace NotifyBubble 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     // This method is called by the Set accessor of each property. 
     // The CallerMemberName attribute that is applied to the optional propertyName 
     // parameter causes the property name of the caller to be substituted as an argument. 
     private void NotifyPropertyChanged(String propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = this; 
      Person = new Person(); 
      Person.Salary = 100; 
     } 
     void PersonPropertyChanged(object sender, PropertyChangedEventArgs e) 
     { 
      if (e.PropertyName == "Salary") 
       NotifyPropertyChanged("CalculatedProperty"); 
     } 
     public Double CalculatedProperty 
     { 
      get 
      { 
       return Person.Salary/2; 
      } 
     } 
     private Person _person; 
     public Person Person 
     { 
      get { return _person; } 
      set 
      { 
       if (Equals(value, _person)) return; 

       if (_person != null) 
        _person.PropertyChanged -= PersonPropertyChanged; 

       _person = value; 

       if (_person != null) 
        _person.PropertyChanged += PersonPropertyChanged; 
       NotifyPropertyChanged("Person"); 
      } 
     } 
    } 
    public class Person: INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     // This method is called by the Set accessor of each property. 
     // The CallerMemberName attribute that is applied to the optional propertyName 
     // parameter causes the property name of the caller to be substituted as an argument. 
     private void NotifyPropertyChanged(String propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     private double salary = 0; 
     public double Salary 
     { 
      get { return salary; } 
      set 
      { 
       if (salary == value) return; 
       salary = value; 
       NotifyPropertyChanged("Salary"); 
      } 
     } 
    } 
} 
0

단지입니다 레코드, 이것은 어떻게 내가 같은 문제를 구현하지만 컬렉션 (다른 예제를 사용)

public class Fields : ObservableCollection<Field> 
{ 
    protected override event PropertyChangedEventHandler PropertyChanged; 

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

    void FieldPropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if (e.PropertyName == "IsSelected") 
     { 
      NotifyPropertyChanged("SelectedItemsCount"); 
     } 
    } 

    protected override void InsertItem(int index, Field item) 
    { 
    if (item != null) 
      item.PropertyChanged -= FieldPropertyChanged; 

     base.InsertItem(index, item); 

     if (item != null) 
      item.PropertyChanged += FieldPropertyChanged; 
    } 

    protected override void ClearItems() 
    { 
     foreach (Field field in this) 
     { 
      field.PropertyChanged -= FieldPropertyChanged; 
     } 
     base.ClearItems(); 
    } 

    protected override void RemoveItem(int index) 
    { 
     if (this[index] != null) 
      this[index].PropertyChanged -= FieldPropertyChanged; 

     base.RemoveItem(index); 
    } 

    private int selectedItemsCount; 

    public int SelectedItemsCount 
    { 
     //This can be more efficient, not have to count everytime 
     get 
     { 
      selectedItemsCount = 0; 

      foreach (Field field in this) 
      { 
       if (field.IsSelected) 
        selectedItemsCount++; 
      } 

      return selectedItemsCount; 
     } 
    } 


} 
관련 문제