如何使用 INotifyDataErrorInfo 接口验证可观察集合
本文关键字:观察 集合 验证 接口 何使用 INotifyDataErrorInfo | 更新日期: 2023-09-27 18:31:48
我正在使用MVVM
模式并使用Prism
框架开发WPF
应用程序。
我有一个基本的数据类如下。
public class ProductDecorator : DecoratorBase<Product>
{
private string _ProductShortName;
private Boolean _IsSelected = false;
// I have omitted some code for clarity here.
[Required]
public int ProductID
{
get { return BusinessEntity.ProductID; }
set
{
SetProperty(() => BusinessEntity.ProductID == value,
() => BusinessEntity.ProductID = value);
}
}
public Boolean IsSelected
{
get { return _IsSelected; }
set
{
SetProperty(ref _IsSelected, value);
}
}
}
我在视图模型中创建了上述数据类的可观察集合。
public class SaleInvoiceViewModel {
private ObservableCollection<ProductDecorator> _productDecorators;
public ObservableCollection<ProductDecorator> ProductDecorators
{
get { return _productDecorators; }
set { SetProperty(ref _productDecorators, value); }
}
}
我将这个可观察集合绑定到视图中的列表框中。
<telerik:RadListBox ItemsSource="{Binding ProductDecorators}" HorizontalAlignment="Stretch" Margin="5,10,5,5" Grid.Column="1" VerticalAlignment="Top">
<telerik:RadListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Margin="2" IsChecked="{Binding IsSelected}" />
<TextBlock Text="{Binding ProductShortName}" FontSize="14" />
</StackPanel>
</DataTemplate>
</telerik:RadListBox.ItemTemplate>
</telerik:RadListBox>
从上面的上下文中,我想验证"用户必须在列表框中至少选择一个项目"。换句话说,IsSelected
属性在可观察集合ProductUmDecorators
的ProductUmDecorator
类之一中必须为 true。
目前我使用INotifyDataErrorInfo
接口和Data Annotations
作为验证规则。我已经失去了我应该如何实现我的问题来实现此验证?
有很多关于堆栈溢出的问题与这个主题相关,但没有可靠的答案。所以我决定发布我的解决方案作为这个问题的答案。问题的上下文是检查"用户必须在列表框中选择一个与可观察集合绑定的项目"。
第一步,ObservableCollection
中的项(实体)需要IsSelected
属性。
public class ProductDecorator : DecoratorBase<Product>
{
private string _ProductShortName;
private Boolean _IsSelected = false;
// I have omitted some code for clarity here.
public Boolean IsSelected
{
get { return _IsSelected; }
set
{
SetProperty(ref _IsSelected, value);
}
}
}
第二步,ObservableCollection
中的每个项目都必须实现INotifyPropertyChanged
接口。然后,您可以访问PropertyChanged
事件处理程序。
第三步,需要将以下方法附加到ObservableCollection
的事件处理程序CollectionChanged
。
public class SaleInvoiceViewModel {
private ObservableCollection<ProductDecorator> _productDecorators;
public ObservableCollection<ProductDecorator> ProductDecorators
{
get { return _productDecorators; }
set { SetProperty(ref _productDecorators, value); }
}
public SaleInvoiceViewModel() {
_productDecorators= new ObservableCollection<ProductDecorator>();
_productDecorators.CollectionChanged += ContentCollectionChanged;
}
public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach(ProductDecorator item in e.OldItems)
{
//Removed items
item.PropertyChanged -= EntityPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach(ProductDecorator item in e.NewItems)
{
//Added items
item.PropertyChanged += EntityPropertyChanged;
}
}
}
}
仔细查看上面的代码EntityPropertyChanged
方法。每当ObservableCollection
中的任何项中的任何属性发生更改时,都会触发此方法。然后,只需在EntityPropertyChanged
方法中调用Validate
方法即可。
private void EntityPropertyChanged( object sender, PropertyChangedEventArgs e )
{
if (e.PropertyName == "IsSelected")
this.Product.ValidateProperty("ProductUmDecorators");
}
如果更改的属性为 IsSelected
,则将运行 ValidatedProperty
方法。我将省略ValidateProperty
方法的详细实现。此方法将尝试使用 DataAnnotations
验证属性,并在出现任何错误时触发 ErrorChanged 事件。您可以在此处了解详细信息。
最后,您需要在实体/视图模型/装饰器类中实现自定义DataAnnotation
ValidationAttribute
,其中您的ObservableCollection
属性存在,如以下代码所示。
public class SaleInvoiceViewModel {
private ObservableCollection<ProductDecorator> _productDecorators;
[AtLeastChooseOneItem(ErrorMessage = "Choose at least one item in the following list.")]
public ObservableCollection<ProductDecorator> ProductDecorators
{
get { return _productDecorators; }
set { SetProperty(ref _productDecorators, value); }
}
}
public class AtLeastChooseOneItem : ValidationAttribute
{
protected override ValidationResult IsValid( object value, ValidationContext validationContext )
{
ProductDecorator tmpEntity = (ProductDecorator) validationContext.ObjectInstance;
var tmpCollection = (ObservableCollection<ProductUmDecorator>) value;
if ( tmpCollection.Count == 0 )
return ValidationResult.Success;
foreach ( var item in tmpCollection )
{
if ( item.IsSelected == true )
return ValidationResult.Success;
}
return new ValidationResult( ErrorMessage );
}
}
这是一个有点复杂的解决方案,但这是我迄今为止发现的最可靠的解决方案。