2013-11-21 3 views
0

다음 문제가 발생했습니다.데이터 설정 컨텍스트

일부 데이터가있는 서버에서 JSON 파일을 가져 왔습니다. 그리고 난 나를 JSON 직렬화 할 수있는 클래스를 작성했습니다 :

public class Person : INotifyPropertyChanged 
{ 
    //public event PropertyChangedEventHandler PropertyChanged; 
    public int? version { get; set; } 
    public int? id { get; set; } 
    public string name { get; set; } 
    public bool? isNeeded { get; set; } 

    //protected void OnPropertyChanged(string name) 
    //{ 
    // PropertyChangedEventHandler handler = PropertyChanged; 
    // if (handler != null) 
    // { 
    //  handler(this, new PropertyChangedEventArgs(name)); 
    // } 
    //} 
} 

을 그리고 난 내가 사람에게 DataContext를 설정 사용자 지정 컨트롤이 - 작품을 바인딩,하지만 난 반응 제어 할 때 일부 필드 (1 또는 다수)가 수정됩니다.

1) 각 속성에 PropertyChanged를 써야합니까?

나는이 작성 관리해야 :

public class Person : INotifyPropertyChanged 
{ 
    private string _name; 
    public event PropertyChangedEventHandler PropertyChanged; 
    public int? version { get; set; } 
    public int? id { get; set; } 
    public string name 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
      OnPropertyChanged("name"); 
     } 
    } 
    public bool? isNeeded { get; set; } 

    protected void OnPropertyChanged(string _name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(_name)); 
     } 
    } 
} 

을하지만 문제는 내가 각 속성에 대한 개인 변수를 생성해야하고 내가 EventChanged 각 속성에 대해 작성해야? 아니면 더 간단한 방법이 있습니까?

+0

실패 할 때 어떤 오류 메시지가 무엇입니까? –

+0

나는이 전투에서 승리 할 수 ​​있었다. 나는 사적인 변수를 추가했다. – Cheese

답변

0

이 솔루션을 찾았

public class Person : INotifyPropertyChanged 
{ 
private string _name; 
private int? _version; 
public event PropertyChangedEventHandler PropertyChanged; 
public int? version 
    { 
     get { return _version; } 
     set { SetField(ref _version, value, "version"); } 
    } 
public int? id { get; set; } 
public string name 
    { 
     get { return _name; } 
     set { SetField(ref _name, value, "name"); } 

    } 
public bool? isNeeded { get; set; } 

protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    protected bool SetField<T>(ref T field, T value, string propertyName) 
    { 
     if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
     field = value; 
     OnPropertyChanged(propertyName); 
     return true; 
    } 

}