0

DataGrid의 유효성 검사와 관련하여 문제가 있습니다. 모델 클래스에서 IDataErrorInfo 유효성 검사를 사용하고 있습니다.CellTemplate 및 CellEditingTemplate이있는 DataGrid의 IDataErrorInfo 유효성 검사

문제는 별도의 CellTemplate 및 CellEditingTemplate 편집 가능한 데이터 그리드에 (참고하지 않은 널 (null) 속성입니다 - 검증은 null 또는 비어있는 경우 오류를 반환) :

저장 버튼을
<!-- some other validated columns --> 
<DataGridTemplateColumn Header="Note"> 
    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate>          
      <TextBox Name="textBoxNote" Text="{Binding Note, ValidatesOnDataErrors=True}" />          
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Note}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

나는 MyObject.Error 유효성을 확인 속성을 반환하고 null이 아니면 MessageBox를 표시합니다. 문제는 첫 번째 열 (노트 하나가 아닌)을 유효한 값으로 변경 한 다음 저장 버튼을 클릭하면 .Error 속성이 null입니다. Note 속성의 ValidatesOnDataError와의 바인딩이 발생하지 않았으므로 (예상하지 못한) 예상되는 동작입니다. (TextBox 컨트롤은 결코 존재하지 않았습니다!). 그러나 TextBlock에서 ValidatesOnDataErrors를 true로 설정하면 DataGrid에 표시되는 모든 객체 (예 : 데이터베이스)에서 원하지 않는 유효성 검사를 받게됩니다. 유효성 검사도이 경우에 많은 시간이 걸릴 수 있습니다 ...

이 문제를 해결하는 올바른 방법은 무엇입니까? 모델 클래스에서 유효성을 유지하고 싶습니다 (개체가 유효한지 여부를 알아야합니다). 코드 숨김 (버튼 이벤트 저장)에서 행 바인딩 객체의 유효성을 강제로 검사 할 수있는 방법이 있습니까? 또는 어떻게 든 초기화해야합니다. 객체 생성시 오류가 있습니까? 다른 아이디어?

편집 : 전체 행 (모든 셀)을 편집 모드 (CellEditingTemplate)에 넣을 수 있습니까? 그런 다음, 모든 컨트롤을로드 할 것이고, 데이터 바인딩, 이것은 또한 ... 모든

감사합니다, 는 DB

답변

0

좋아, 내가 IDataErrorInfo 객체를 재 검증 관리 검증 의미 - 강제 IDataErrorInfo 검증의 종류. 그렇지 않으면 DataGrid에 새 객체를 추가 할 수 있었지만 속성 (편집 된 객체 제외)은 유효성이 확인되지 않았습니다. 사용자가 데이터 그리드에 새 개체를 추가 할 때 내가 수동으로 설정하는 myNewObject.Revalidate() 메서드를 호출 이제

public virtual void Revalidate() // never needed to override though 
{ 
    Type type = this.GetType(); 

    // "touch" all of the properties of the object - this calls the indexer that checks 
    // if property is valid and sets the object's Error property 
    foreach (PropertyInfo propertyInfo in type.GetProperties()) 
    {     
     var indexerProperty = this[propertyInfo.Name]; 
    } 
} 

: 내 모델 객체 (즉 IDataErrorInfo를 확장)의 모든 슈퍼 클래스에서

나는이 방법을 추가 개체를 데이터베이스에 저장하기 전에 확인하는 Error 속성 어쩌면 이것이 최선의 해결책은 아닐지 모르지만 그것은 매우 고통스럽지 않습니다.

감사합니다. DB

관련 문제