Silverlight ValidationSummary未处理ValidationException

Silverlight ValidationSummary未处理ValidationException,validation,silverlight-4.0,observablecollection,Validation,Silverlight 4.0,Observablecollection,我在数据表单中有一个数据表单和一个数据网格。此datagrid绑定到ObservableCollection。我编写了一个CustomValidator,当可观察集合中的计数为0时抛出ValidationException。ValidationSummary控件不处理此异常,相反,应用程序变得不稳定并调用应用程序未处理的异常。我没有使用RIA服务。下面是我的代码 public class UserCompanyProgram : INotifyPropertyChanged { publ

我在数据表单中有一个数据表单和一个数据网格。此datagrid绑定到ObservableCollection。我编写了一个CustomValidator,当可观察集合中的计数为0时抛出ValidationException。ValidationSummary控件不处理此异常,相反,应用程序变得不稳定并调用应用程序未处理的异常。我没有使用RIA服务。下面是我的代码

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 });
public类UserCompanyProgram:INotifyPropertyChanged
{
公共空塔服务程序()
{
lstUserProgram.CollectionChanged+=(发件人,e)=>
{
//Validator.ValidateProperty(lstUserProgram,
//新的ValidationContext(this,null,null){MemberName=“lstUserProgram”});
lstUserProgram=lstUserProgram;
UserProgramChanged();
};
}
私有ObservableCollection _lstUserProgram=新ObservableCollection();
[CustomValidation(typeof(ModelValidator),“ValidateUserProgramCollection”)]
[显示(Name=“新建程序”,Description=“添加程序”)]
公共可观测收集程序
{
获取{return}lstUserProgram;}
设置
{
Validator.ValidateProperty(lstUserProgram,
新的ValidationContext(this,null,null){MemberName=“lstUserProgram”});
此.lstUserProgram=值;
NotifyPropertyChanged(“lstUserProgram”);
}
}
}
dgSelectedPrograms.SetBinding(DataGrid.ItemsSourceProperty,新绑定(“lstUserProgram”){validateAnnotifyDataErrors=true,validateNexceptions=true});

如何使ValidationSummary处理异常?

我在项目中遇到了一个类似的问题,我在文本框的keyup事件上强制进行验证。我在keyup事件中将textbox绑定属性设置为textbox中的文本,以便在他们键入时进行验证。Silverlight不喜欢这样。它正在抛出一个未处理的异常。因此,一旦我从keyup事件中删除了验证,它就会正常工作。在用户选择屏幕上的另一个控件之前,属性不会得到更新,但是如果我想利用Silverlight验证,这就是需要做的

我还没有验证一个集合,比如您正在做什么,但是在这种情况下,您可能需要使用IDataErrorInfo实现。在这种情况下不会抛出异常,因此这可能就是解决方法