- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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);
}
}
}
我在 ViewModel 中创建了上述数据类的可观察集合。
public class SaleInvoiceViewModel {
private ObservableCollection<ProductDecorator> _productDecorators;
public ObservableCollection<ProductDecorator> ProductDecorators
{
get { return _productDecorators; }
set { SetProperty(ref _productDecorators, value); }
}
}
然后我将这个可观察集合限制在 View 中的列表框中。
<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
属性在 可观察集合 的 ProductUmDecorator
类之一中必须为 true ProductUmDecorators
.
目前我使用 INotifyDataErrorInfo
接口(interface)和 Data Annotations
作为验证规则。我忘记了我应该如何实现我的问题来实现此验证?
最佳答案
在 stackoverflow 上有很多与此主题相关的问题,但没有可靠的答案。所以我决定发布我的解决方案作为这个问题的答案。问题的上下文是检查“用户必须在与可观察集合绑定(bind)的列表框中选择一项”。
第一步,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
接口(interface)。然后您可以访问 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 事件。可以详细了解here .
最后,您需要在您的 Entity/ViewModel/Decorator 类中实现自定义 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 );
}
}
这是一个有点复杂的解决方案,但这是迄今为止我找到的最可靠的解决方案。
关于c# - 如何使用 INotifyDataErrorInfo 接口(interface)验证 Observable 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34973407/
在绑定(bind)的情况下,例如 哪些类需要实现 INotifyDataErrorInfo: 数据上下文 一些项目 另一个项目 这些的一些组合 最佳答案 AnotherItem INotifyDat
我有点困惑 MSDN example . 目前尚不清楚如何处理和设置实体相关错误。 示例代码: public System.Collections.IEnumerable GetErrors(stri
我有一个 ObservableCollection 类型的数据集合(比如实例作为 myClassTypes)。在一些用户操作之后,这个 myClassTypes 填充了 ViewModel 中的值。在
UWP平台控件是否通过绑定(bind)自动支持INotifyDataErrorInfo接口(interface)? 在 Silverlight 和 WPF 上,如果我们实现 INotifyDataEr
我遇到了一个非常困惑的情况: 我打开了一个对话框,其中显示了一个带有 INotifyDataErrorInfo 的 View ,它立即返回一个错误(当文本字段不为空时),我看到了红色边框的错误通知:
我在 WPF 中使用 INotifyDataError 接口(interface)进行异步验证。我有属性(property) 在我的 View 模型上,我有一个属性 public SomeType
当 TextBox 为空时,我有一个简单的验证来显示错误消息。问题在于消息仅显示消息的第一个 字母。 在文本框样式中: 如果我将错误消息直接设置为 Setter 值,它会毫无问题地显示所
我正在尝试实现 INotifyDataErrorInfo,但在尝试验证 ObservableCollection 属性时没有成功。 问题是,如果集合有误,我会得到红色边框,但如果我更正集合,红色边框就
我正在尝试实现 INotifyDataErrorInfo,我的模型有一些自定义类型,需要根据其使用情况进行不同的验证。我不确定如何实现此验证。 我试图在下面创建一个简单的示例来展示我正在尝试完成的工作
我完全按照以下链接中的描述实现了 INotifyDataErrorInfo: http://blog.micic.ch/net/easy-mvvm-example-with-inotifyproper
我有一个实现 INotifyDataErrorInfo 的 View 模型。我将一个文本框绑定(bind)到这样的 View 模型属性之一: 数据绑定(bind)有效,但是当我添加如下验证错误时 U
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我应该使用 IDataErrorInfo、INotifyDataErrorInfo 还是两者都使用? 如果我同时使用两者,我应该在两者中提供相同的错误,还是只提供来自 IDataErrorInfo 的
上下文 我正在使用 MVVM 和 Entity Framework (数据库优先)开发 WPF 应用程序。我有一个机身对象的 ObservableCollection(通过 CollectionVie
我有一个实现 INotifyPropertyChanged 的模型和 INotifyDataErrorInfo .每当我修改了属性时,就会触发 Property changed 事件,但由于某种原因,
我是 WPF 新手,开始学习下面的教程。 http://social.technet.microsoft.com/wiki/contents/articles/19490.validating-dat
我正在使用 INotifyDataErrorInfo 接口(interface)来实现通用的 MVVM 验证机制。我通过调用 OnValidate 而不是 OnPropertyChanged 来实现接
我正试图找到一个优雅的解决方案来使用 Caliburn.Micro MVVM 框架实现 INotifiyDataErrorInfo。 我想限制将在每个需要实现验证的虚拟机中重复的代码量。我首先编写了一
这是一个奇怪的问题,此时我认为这可能与我的机器配置有关。 基本上我已经创建了一个非常标准的 INotifyDataErrorInfo 实现。在某些情况下,当提高 ErrorsChanged事件我得到一
我想我的问题是validationsummary到底是如何确定它要显示的内容的? 我有一个实现 INotifyDataErrorInfo 的 View 模型,一些验证错误显示在我的验证摘要中,而其他错
我是一名优秀的程序员,十分优秀!