2016-07-11 2 views
2

아래와 같이 idataerrorinfo를 구현했지만 작동하지 않는다면 모두 도와 줄 수 있습니까? 디버깅 할 때 화면에 오류가 나타나지 않습니다.idataerrorinfo in C# WPF

모델 부분은 :

class Person : Viewmodelbase, INotifyPropertyChanged{ 
    private string name; 
    private string age; 
    public string Name{ 
     get 
     { 
      return name; 
     } 

     set 
     { 
      name = value; 
      if (string.IsNullOrWhiteSpace(value)) 
      { 
       this.seterror("Name", "Please Input sth"); 
      } 
      else 
       clearerror("Name"); 
      OnpropertyChanged("name"); 
     } 
    } 
} 

Viewmodelbase 부분이다

class Viewmodelbase : INotifyPropertyChanged, IDataErrorInfo 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void onpropertychange(string property) 
    { 
     if (string.IsNullOrEmpty(property)) 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
    } 

    string IDataErrorInfo.Error 
    { 
     get 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    private Dictionary<string, string> error1 = new Dictionary<string, string>(); 

    public string this[string columnName] 
    { 
     get 
     { 
      if (error1.ContainsKey(columnName)) 
       return error1[columnName]; 
      return null; 

     } 
    } 

    public void seterror(string key1,string value1) 
    { 
     this.error1[key1] = value1; 
     this.onpropertychange("HasError"); 
    } 

    public void clearerror(string key1) 
    { 
     if (this.error1.ContainsKey(key1)) 
     { 
      this.error1.Remove(key1); 
      this.onpropertychange("HasError"); 
     } 
    } 

    protected void ClearErrors() 
    { 
     this.error1.Clear(); 
     this.onpropertychange("HasError"); 
    } 

    public bool HasError 
    { 
     get 
     { 
      return this.error1.Count != 0; 
     } 
    } 


} 

뷰 모델 부분이다

class MainWindowViewModel : Viewmodelbase,INotifyPropertyChanged 
{ 
    public Person myboy 
    { 
     get; 
     set; 
    } 
} 

WPF 부분이다

<TextBox Width="250" FontSize="20" VerticalAlignment="Center" DataContext="{Binding Source={StaticResource VM}}" Text="{Binding Path=myboy.Name,Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True,ValidatesOnExceptions=True}" Name="textbox1" Margin="0,199"></TextBox> 
+0

'새로운 NotImplementedException 던져() : 나는 당신과 같은 코드를 게시 할 경우 질문에 대한 답을 귀찮게하지 않습니다 다음에) 여기

당신을위한 영감이다. – Will

답변

1

C#은 대소 문자를 구분합니다.

는 속성 이름을 소문자를 사용하는 것 같습니다 :

public string name{ get{..} set{..} } 

을 있지만 오류 저장할 때 낙타 표기법을 사용합니다 속성과 메소드

this.seterror("Name", "Please Input sth"); 

사용 CamelCase를합니다.

clearerror("Name"); 
OnpropertyChange("name"); 

, 명명 규칙을 따르십시오 :

심지어 propertyName 형식이 일치하지 않습니다 seterror RESP cleanerrorOnpropertychanged에 전달. 명명 규칙이 의미가 있습니다. ;`모든 facepalms

private string _name; //or `private string name;` but I recommend underscore for private fields. 
public string Name{get; set;} 
public Person MyBoy{get; set;} 
public void OnPropertyChanged(string propertyName); //propertyChange and PropertyChanged has different meaning, so use the correct one 
public void SetError(string propertyName, string error){..} //parameter names key and value are useless, they say nothing about what the parameters actually are. 
+0

그의 코드에'NotImplementedException();를 던져 보셨습니까? 그의 구현에 관한 문제 일 수도 있습니다 : | – Will

+0

이것이 실제로 WPF 프레임 워크에 의해 호출되는지 확실하지 않습니다. 그렇지 않으면 프로그램을 깨뜨릴 것입니다 – Liero

+0

대답을 주셔서 감사합니다 –