验证命令
RadDataGrid控件提供了一个验证命令,该命令具有用于验证单元格内容的入口点。执行参数为typeValidateCellContext它公开了以下属性:
- CellInfo:获取与操作关联的单元格信息。
- 错误:获取或设置验证期间发生的错误(如果有)。
例子
下面是RadDataGrid ValidateCell命令的工作示例:
类继承的类Data(我们的业务对象)INotifyDataErrorInfo而且INotifyPropertyChanged接口。
我们将通过INotifyDataErrorInfo接口。
公共类数据:INotifyDataErrorInfo, INotifyPropertyChanged{私有字典> errors =新字典>();私弦国;国家{获取{返回此国家;}设置{this。国家=价值;如果(this.country。长度> 15){这个。AddError(“国家”字符串。格式("国家太长",新的DateTime()));} else {this.RemoveErrors("Country"); } this.OnPropertyChanged("Country"); } } public string Capital { get; set; } public bool HasErrors { get { return this.errors.Count > 0; } } public IEnumerable GetErrors(string propertyName) { if (string.IsNullOrEmpty(propertyName)) { return this.errors.SelectMany(c => c.Value); } HashSet
然后创建一个包含数据对象集合的ViewModel:
公共类ViewModel: INotifyPropertyChanged {public ObservableCollection Items {get;设置;} public ViewModel() {this。item = new ObservableCollection(){新数据{国家= "印度",首都= "新德里"},新数据{国家= "南非",首都= "开普敦"},新数据{国家= "尼日利亚",首都= "阿布贾"},新数据{国家= "新加坡",首都= "新加坡"}};}公共事件PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(字符串名称){PropertyChangedEventHandler处理程序= PropertyChanged;if (handler != null) {handler(this, new PropertyChangedEventArgs(name));}}}
然后将CellTap操作作为命令处理。首先,创建一个继承于DataGridCommand的类,并相应地设置它的Id属性。你还需要重写CanExecute和Execute方法,如下面的例子所示:
公共类ValidateCellCommand: DataGridCommand{网格网格;public ValidateCellCommand(Grid Grid) {this。Id = DataGridCommandId.ValidateCell;这一点。网格=网格;} public override void执行(对象参数){var context = (ValidateCellContext)参数;this.grid.IsVisible = context.Errors.Count > 0;}}
然后将BindingContext设置为ViewModel,并将此命令添加到RadDataGrid实例的Commands集合中:
这一点。BindingContext = new ViewModel();this.dataGrid.Commands。添加(新ValidateCellCommand (errorContainer));
在XAML中定义RadDataGrid:
浏览器应用程序包含一个演示如何使用ValidateCell命令的示例。验证示例位于DataGrid/Commands文件夹中。