2011-02-03 58 views
1

我在數據表單中有一個dataform和一個datagrid。這個數據網格綁定到一個ObservableCollection。我寫了一個CustomValidator,當可觀察集合中的計數爲0時拋出ValidationException。ValidationSummary控件不處理此異常,而是應用程序變得不穩定並調用Application Unhandled Exception。我沒有使用RIA服務。以下是我的代碼Silverlight驗證摘要不處理ValidationException

public class UserCompanyProgram : INotifyPropertyChanged 
{ 
    public void ToWebServiceProgram() 
    { 
     lstUserProgram.CollectionChanged += (sender, e) => 
     { 
      //Validator.ValidateProperty(lstUserProgram, 
      //    new ValidationContext(this, null, null) { MemberName = "lstUserProgram" }); 
      lstUserProgram = _lstUserProgram; 
      UserProgramChanged(); 
     }; 
    } 
    private ObservableCollection<Pricing.Model.UserProgram> _lstUserProgram = new ObservableCollection<UserProgram>(); 
    [CustomValidation(typeof(ModelValidator), "ValidateUserProgramCollection")] 
    [Display(Name = "New Programs", Description = "Add program")] 
    public ObservableCollection<UserProgram> lstUserProgram 
    { 
     get { return _lstUserProgram; } 
     set 
     { 
       Validator.ValidateProperty(lstUserProgram, 
       new ValidationContext(this, null, null) { MemberName = "lstUserProgram" }); 
       this._lstUserProgram = value; 
       NotifyPropertyChanged("lstUserProgram"); 

     } 
    } 
} 

    dgSelectedPrograms.SetBinding(DataGrid.ItemsSourceProperty, new Binding("lstUserProgram") { ValidatesOnNotifyDataErrors=true, ValidatesOnExceptions=true }); 

如何使ValidationSummary句柄成爲異常?

回答

0

我在我的項目也有類似的問題,我在那裏迫使在文本框的keyup事件中進行驗證。我在文本框中將文本框綁定屬性設置爲keyup事件中的文本,所以我可以在鍵入時進行驗證。 Silverlight不喜歡這個。它拋出一個未處理的異常。所以一旦我從keyup事件中刪除了驗證,它就像它應該那樣工作。在用戶選擇屏幕上的另一個控件之前,該屬性不會更新,但如果我想利用Silverlight驗證,則需要完成此操作。

我還沒有確認一個集合的工作,例如你在做什麼,但也許這樣你需要使用IDataErrorInfo的執行情況。在這種情況下不會拋出異常,所以這可能是一種方式。