2010-11-25 7 views
4

Im DataAnnotation을 사용하여 입력 컨트롤의 유효성을 검사합니다. 그러나 ValidatesOnExceptions는 사용자가 텍스트 상자에 무엇인가를 입력하고 Tab을 누르면 작동합니다. (기본적으로 Lostfocus 이벤트).Silverlight에서 버튼을 클릭 할 때 입력의 유효성을 검사하는 방법은 무엇입니까?

하지만 사용자가 입력란에 아무 것도 입력하지 않고 제출을 클릭하면 작동하지 않습니다. ASP.NET Page.IsValid 속성과 마찬가지로 Silverlight에서 사용할 수있는 모든 속성이나 메서드가 UI에 대한 모든 컨트롤의 유효성을 검사합니까?

답변

0

페이지에 표시되는 모든 UserControls의 유효성을 검사하는 방법이 있다고 생각하지 않습니다. 그러나 INotifyDataErrorInfo를 살펴 보는 것이 좋습니다. 이것은 내 생각에 실버 라이트의 데이터를 검증하는 가장 좋은 방법입니다. INotifyDataErrorInfo 접근법을 사용하면 (ValidatesOnException, ...)와 같이 뷰를 변경할 필요가없고 쉬운 방법으로 WebService에 대해 유효성을 검사 할 수 있습니다 (데이터 어노테이션에서는 불가능 함). 이 당신을 도와 http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-binding-with-inotifydataerrorinfo.aspx

희망 :

여기를 보라.

+0

INotifyDataErrorInfo는 실제로 Silverlight MVVM 유효성 검사를위한 접근 방식이지만 Button 클릭에 대한 유효성 검사 컨트롤 문제를 해결하지 못합니다. –

1

Terence에서 제공 한 URL에서 도움을 얻으면 아래 해결책을 준비했습니다. 서비스 호출 전에 모든 속성이 설정되어 있는지 확인할 수 있습니다.

public class PersonViewModel : EntityBase 
{ 
    private readonly RelayCommand saveCommand; 

    public PersonViewModel(IServiceAgent serviceAgent) 
    { 
     saveCommand = new RelayCommand(Save) { IsEnabled = true }; 
    } 

    public RelayCommand SaveCommand // Binded with SaveButton 
    { 
     get { return saveCommand; } 
    } 

    public String Name // Binded with NameTextBox 
    { 
     get 
     { 
      return name; 
     } 
     set 
     { 
      name = value; 
      PropertyChangedHandler("Name");     
      ValidateName("Name", value); 
     } 
    } 

    public Int32 Age // Binded with AgeTextBox 
    { 
     get 
     { 
      return age; 
     } 
     set 
     { 
      age = value; 
      PropertyChangedHandler("Age"); 
      ValidateAge("Age", value); 
     } 
    } 

    private void ValidateName(string propertyName, String value) 
    { 
     ClearErrorFromProperty(propertyName); 
     if (/*SOME CONDITION*/)  
      AddErrorForProperty(propertyName, "/*NAME ERROR MESSAGE*/");   
    } 

    private void ValidateAge(string propertyName, Int32 value) 
    { 
     ClearErrorFromProperty(propertyName); 
     if (/*SOME CONDITION*/)  
      AddErrorForProperty(propertyName, "/*AGE ERROR MESSAGE*/");    
    } 

    public void Save() 
    { 
     ValidateName("Name", name); 
     ValidateAge("Age", age);   
     if (!HasErrors) 
     {     
      //SAVE CALL TO SERVICE 
     } 
    }  
} 
관련 문제