接口 IDataErrorInfo 不工作
Interface IDataErrorInfo isn't working
我有以下 class 实现了 IDataErrorInfo
接口,但它不起作用,即它不进行验证。代码看起来很完美。我不知道为什么。我设置了一个断点,它甚至没有进入 IDataErrorInfo Members
区域。
产品class
[DataContract()]
public class Product : IDataErrorInfo
{
[DataMember()]
public string Name{get;set;}
[DataMember()]
public string Code{get;set;}
#region IDataErrorInfo Members
public string Error
{
get
{
return null;
}
}
public string this[string property]
{
get
{
switch (property)
{
case "Name":
if (string.IsNullOrEmpty(Name))
return "Name is required";
break;
case "Code":
if (string.IsNullOrEmpty(Code))
return "Code is required";
break;
default:
break;
}
return null;
}
}
#endregion
public Product(string name, string code)
{
Name = name;
Code = code;
}
}
XAML 用于绑定 textbox
<TextBox Grid.Column="1"
HorizontalAlignment="Left"
Height="23"
Margin="24,9,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="148" x:Name="txtName"
Text="{Binding Name,Mode=TwoWay,ValidatesOnDataErrors=True}"
MaxLength="50"/>
您需要使用 INotifyPropertyChanged
和 IDataErrorInfo
使您的对象可观察,以便绑定知道属性已更改并检查 [=15 时是否有任何错误=]
public class Product : IDataErrorInfo, INotifyPropertyChanged {
string _name;
[DataMember()]
public string Name{
get { return _name; }
set {
_name = value;
NotifyPropertyChanged("Name");
}
}
//...Other code removed for brevity
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
您甚至可以将 属性 更改的功能移出到基础 class 中以便像这样重用
public abstract class PropertyChangedBase: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
并像
一样使用它
public class Product : PropertyChangedBase, IDataErrorInfo {
//Code removed for brevity
}
我有以下 class 实现了 IDataErrorInfo
接口,但它不起作用,即它不进行验证。代码看起来很完美。我不知道为什么。我设置了一个断点,它甚至没有进入 IDataErrorInfo Members
区域。
产品class
[DataContract()]
public class Product : IDataErrorInfo
{
[DataMember()]
public string Name{get;set;}
[DataMember()]
public string Code{get;set;}
#region IDataErrorInfo Members
public string Error
{
get
{
return null;
}
}
public string this[string property]
{
get
{
switch (property)
{
case "Name":
if (string.IsNullOrEmpty(Name))
return "Name is required";
break;
case "Code":
if (string.IsNullOrEmpty(Code))
return "Code is required";
break;
default:
break;
}
return null;
}
}
#endregion
public Product(string name, string code)
{
Name = name;
Code = code;
}
}
XAML 用于绑定 textbox
<TextBox Grid.Column="1"
HorizontalAlignment="Left"
Height="23"
Margin="24,9,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="148" x:Name="txtName"
Text="{Binding Name,Mode=TwoWay,ValidatesOnDataErrors=True}"
MaxLength="50"/>
您需要使用 INotifyPropertyChanged
和 IDataErrorInfo
使您的对象可观察,以便绑定知道属性已更改并检查 [=15 时是否有任何错误=]
public class Product : IDataErrorInfo, INotifyPropertyChanged {
string _name;
[DataMember()]
public string Name{
get { return _name; }
set {
_name = value;
NotifyPropertyChanged("Name");
}
}
//...Other code removed for brevity
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
您甚至可以将 属性 更改的功能移出到基础 class 中以便像这样重用
public abstract class PropertyChangedBase: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
并像
一样使用它public class Product : PropertyChangedBase, IDataErrorInfo {
//Code removed for brevity
}