2013-07-26 51 views
5

Dường như không biết tôi đang đi đâu sai? các OnPropertyChange không được recondnised bất kỳ đề xuất?OnPropertyChange không tồn tại trong ngữ cảnh hiện tại?

public class MedicationList : INotifyPropertyChanged 
{ 



    public int MedicationID { get; set; } 

    public string Description 
    { 
     get 
     { 
      return Description; 
     } 
     set 
     { 
      OnPropertyChanged("Description"); 
      Description = value; 

     } 
    } 
} 

}

EDIT Tôi đã thêm public class MedicationList : INotifyPropertyChanged

+1

Bạn đã khai báo phương thức 'OnPropertyChanged' ở bất cứ đâu trong lớp' MedicationList' của bạn chưa? Tôi không thấy nó. –

+5

Đệ quy phát hiện: 'Mô tả = giá trị;' –

+0

Lớp học của bạn phải triển khai giao diện INotifyPropertyChanged –

Trả lời

6

Bạn nên thực hiện giao diện INotifyPropertyChanged, trong đó có đơn PropertyChanged sự kiện công bố. Bạn nên nâng cao sự kiện này nếu một số thuộc tính của đối tượng thay đổi. thực hiện đúng:

public class MedicationList : INotifyPropertyChanged 
{ 
    private string _description; // storage for property value 

    public event PropertyChangedEventHandler PropertyChanged; 

    public string Description 
    { 
     get { return _description; } 
     set 
     { 
      if (_description == value) // check if value changed 
       return; // do nothing if value same 

      _description = value; // change value 
      OnPropertyChanged("Description"); // pass changed property name 
     } 
    } 

    // this method raises PropertyChanged event 
    protected void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) // if there is any subscribers 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

Tôi nghĩ rằng phần quan trọng đối với OP là 'OnPropertyChanged' không xuất hiện một cách kỳ diệu từ không khí mỏng - thậm chí không thêm 'INotifyPropertyChanged' vào danh sách các giao diện được triển khai. –

+0

Tên onPropertychange không tồn tại trong ngữ cảnh hiện tại – Mark

+0

@ Thực sự nó tồn tại, ở dưới cùng của định nghĩa lớp –

2

Tôi đặt cược bạn muốn làm một cái gì đó như thế này:

public class MedicationList : INotifyPropertyChanged { 
    public int MedicationID { get; set; } 
    private string m_Description; 

    public string Description { 
    get { return m_Description; } 
    set { 
     m_Description = value; 
     OnPropertyChanged("Description"); 
    } 
    } 

    private void OnPropertyChanged(string propertyName) { 
    if (string.IsNullOrEmpty(propertyName)) 
     throw new ArgumentNullException("propertyName"); 

    var changed = PropertyChanged; 
    if (changed != null) { 
     changed(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 
+4

Đệ quy cũng được phát hiện ở đây. :) Mô tả = giá trị – Nitesh

+0

@Nitesh - bắt tốt, cố định :) –

0

Phương pháp này cần phải được xác định theo loại của bạn để nâng cao sự kiện INotifyPropertyChanged::PropertyChanged

public PropertyChangedEventHandler PropertyChanged; 

... 

private void OnPropertyChanged(string propertyName) { 
    var saved = PropertyChanged; 
    if (saved != null) { 
    var e = new PropertyChangedEventArgs(propertyName); 
    saved(this, e); 
    } 
} 
1

Bạn cần mã thực tế mà giao diện thực hiện bên trong lớp của bạn.

/// <summary> 
/// Public event for notifying the view when a property changes. 
/// </summary> 
public event PropertyChangedEventHandler PropertyChanged; 

/// <summary> 
/// Raises the PropertyChanged event for the supplied property. 
/// </summary> 
/// <param name="name">The property name.</param> 
internal void OnPropertyChanged(string name) 
{ 
    PropertyChangedEventHandler handler = PropertyChanged; 
    if (handler != null) 
    { 
     handler(this, new PropertyChangedEventArgs(name)); 
    } 
} 
0

Có sự khác biệt giữa lớp cơ sở và giao diện.

Với lớp cơ sở, các thành viên được tự động kế thừa và không cần phải làm gì cả (trừ khi một số thành viên cần override). Với một giao diện, lớp không kế thừa các thành viên giao diện một cách tự động; bạn phải giới thiệu họ trong lớp học của bạn. Nếu bạn không trình biên dịch sẽ phàn nàn.

INotifyPropertyChanged là một giao diện.

Các vấn đề liên quan