- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在研究我的 WPF 应用程序的验证,并看到提到 IDataErrorInfo
.然而,关于如何使用它的指南很少,更糟糕的是,没有人解释它是如何工作的。
在 MSND.com 网站上给出:
MSDN
public class Person : IDataErrorInfo
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public string Error
{
get
{
return null;
}
}
public string this[string name]
{
get
{
string result = null;
if (name == "Age")
{
if (this.age < 0 || this.age > 150)
{
result = "Age must not be less than 0 or greater than 150.";
}
}
return result;
}
}
}
我知道这里发生了什么,但我不知道它对我的数据有什么影响。
Age
as 400:调用属性上的 setter 。错误会阻止它被设置吗?如果不是,它只是警告数字不正确,有什么办法阻止某人按原样保存信息?没有
IsValid()
检查方法,有吗?
最佳答案
Error
通常不使用属性,但您必须定义它才能实现接口(interface)。
正如 decyclone 所说,验证不会阻止属性被设置为错误的值,但您可以将属性设置为默认值。
让我告诉你我是如何使用它的。我有几个 TextBox
我必须验证他们拥有的值(value)。我不想在调用集合时显示带有错误的 MessageBox,而是采用“webly”方法:我想要 TextBox
的边框和背景当设置了无效值并且 TextBox
的工具提示为红色显示它得到的错误。
这是我的文本框 xaml:
<converters:ValidationConverter x:Key="validationConverter"/>
<Style x:Key="TestStepTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource validationConverter}, Path=(Validation.Errors)}"/>
<Setter Property="Background" Value="#33FF342D"/>
<Setter Property="BorderBrush" Value="#AAFF342D"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<TextBox Name="txtRunAfter" Text="{Binding RunAfter, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource TestStepTextBox}"/>
<TextBox Name="txtStopAfter" Text="{Binding StopAfter, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource TestStepTextBox}"/>
UpdateSourceTrigger=PropertyChanged
有关,有一段时间 HasError 属性为 true 但没有设置错误(参见
link )。所以这里是转换器的代码:
public class ValidationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ReadOnlyObservableCollection<ValidationError> errors = value as ReadOnlyObservableCollection<ValidationError>;
if (errors == null) return value;
if (errors.Count > 0)
{
return errors[0].ErrorContent;
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("This method should never be called");
}
}
private int _runAfter = 0;
public int RunAfter
{
get
{
return _runAfter;
}
set
{
if (_runAfter != value)
{
_runAfter = value;
OnPropertyChanged("RunAfter");
if (validateRunAfter() == null)
setRunAfter(); //sets the property value to the model layer
}
}
}
string IDataErrorInfo.this[string columnName]
{
get
{
string message = null;
if (columnName == "RunAfter")
message = validateRunAfter();
//...
return message;
}
}
private string validateRunAfter()
{
if (value >= _order)
return "Run After value must be less than its Step Order (#) value.";
return null;
}
关于wpf - IDataErrorInfo 接口(interface)如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3109244/
我正在使用 MVVM 在 WPF 项目中工作. 使用 IDataErrorInfo 验证错误的最佳做法是什么? ?在我的模型或 View 模型中? 用于实现验证的最佳模式是什么? 附注我正在使用 .N
我正在创建一个将使用 IDataErrorInfo 的 WPF 应用程序我的业务对象实现的数据验证。我一直在使用 this demo from a blog post了解ValidatesOnData
注意-我的类(class)是EntityObject上课! 我有以下类(class): public class Foo { public Bar Bar { get; set; } } pu
在 MVVM IDataErrorInfo用于验证。在这个时间索引器检查值不能得到。在调用它后如何检查我无法获取的值。代码在这里。列名中传递的值是什么。 public string this[stri
我有一个简单定义如下的地址对象: public class Address { public string StreetNumber { get; set; } public stri
自从我尝试学习 WPF 以来,我越来越多地看到使用接口(interface) IDataErrorInfo 将错误绑定(bind)到接口(interface)。我的问题是我通常将数据验证放在属性的 s
我不知道是我找不到它还是它不存在,但是是否有任何验证属性可以检查集合是否为空/空? 如果没有,是否有关于如何创建我自己的验证属性的好资源? 最佳答案 您好,当您按照默认使用 DataAnnotatio
昨天,我问了一个关于 MVVM 验证的问题,有人用一段代码回答:https://stackoverflow.com/a/13387724/ 我试图理解这段代码,但我就是不理解索引器部分。有人可以向我解
我有一个绑定(bind)到 winform 的对象,这个对象实现了 IDataErrorInfo。我有一个错误提供者。问题是当一个属性的一个属性发生变化时。 当我更改年龄时没有问题(即检查并正确显示/
好吧,现在我正在验证数据。我有这段代码: 在我看来:
我在使用 IDataErrorInfo 验证 ComboBox 时遇到问题。 我设置了 1 个文本框和 1 个组合框,在运行程序时,第一个焦点在文本框上,当我点击 Tab 以聚焦在组合框中时,我得到:
这个问题可能很简单,但是我找不到办法做,所以我问了。 我在我的模型中使用 IDataErrorInfo 来验证它如下: public class Group : INotifyPropertyChan
我正在为 WPF 应用程序编写(尝试编写)单元测试。 UI 绑定(bind)以实现 IDataErrorInfo 的业务对象,这样当我在 View xaml 中设置 ValidatesOnDataEr
DataAnnotations 与 IDataErrorInfo 两者的优点和缺点? 一个比另一个的好处? (尤其是与 MVC 相关的) 最佳答案 因为我不想开始一个新问题,所以迟到了讨论。我的出发点
目前我的业务对象实现IDataErrorInfo .由于我打算在 ASP.NET MVC 3 中使用这些库,我认为我应该实现 IValidatableObject也可以代替。 WPF 是否与 IVal
我在某些面板上有两个控件:文本框和组合框: 当我在组合框上选择某些内容时,我需要强制对文本框进行 idataerrorinfo 验证。怎么做? View 模型中的代码: string IDataE
我们在我的 MVVM 应用程序中有一个配置设置页面。此处的字段之一(属性:BackupFolderPath)有一个 TextBox 控件。我们已经使用 IDataErrorInfo 对该控件进行了验证
我是 WPF MVVM 的新手,想对本文提出一个后续问题: Enable Disable save button during Validation using IDataErrorInfo 如果表单
我有 ItemType ,它在 IDataErrorInfo 接口(interface)的帮助下实现了验证所需的一切: #region IDataErrorInfo implementation
我有一个实现 IDataError 的 View 模型。在 View 模型中,我有一个 ObservableCollection。 ObservableCollection 在我的 View 中填充了
我是一名优秀的程序员,十分优秀!