我遇到了IDataErrorInfo的问题,这是一个示例
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
public class MainWindowViewModel : INotifyPropertyChanged, IDataErrorInfo
{
public List<Person> _source1 = null;
public List<Person> Source1
{
get
{
return _source1 ?? (_source1 = new List<Person>()
{
new Person(){ Nom ="one"}
});
}
}
public List<Person> _source2 = null;
public List<Person> Source2
{
get
{
return _source2 ?? (_source2 = new List<Person>()
{
new Person(){ Nom ="two"}
});
}
}
private Person _selectedItem1;
public Person SelectedItem1 { get { return _selectedItem1; } set { _selectedItem1 = value; RaisePropertyChanged("SelectedItem1"); } }
private Person _selectedItem2;
public Person SelectedItem2 { get { return _selectedItem2; } set { _selectedItem2 = value; RaisePropertyChanged("SelectedItem2"); } }
public MainWindowViewModel()
{
//This is the actual way, i solve my problem
//this.PropertyChanged += (sender, e) =>
// {
// if (e.PropertyName == "SelectedItem1")
// {
// RaisePropertyChanged("SelectedItem2");
// }
// };
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get {
string errors= string.Empty;
if (columnName == "SelectedItem2")
{
if(this.SelectedItem1 != null && this.SelectedItem2 == null)
errors = "erreur";
}
return errors;
}
}
}
public class Person
{
public string Nom { get; set; }
}
正如你所看到的,我必须告知SelectedItem2是奇怪的,因为SelectedItem1被选中,但redcorner没有出现。这是xaml代码。
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ComboBox ItemsSource="{Binding Path=Source1}" DisplayMemberPath="Nom" SelectedItem="{Binding Path=SelectedItem1}"/>
<ComboBox ItemsSource="{Binding Path=Source2}" DisplayMemberPath="Nom" SelectedItem="{Binding Path=SelectedItem2,ValidatesOnDataErrors=True}"/>
</StackPanel>
是解决此问题的更好方法。
答案 0 :(得分:2)
我认为您需要做的就是
public Person SelectedItem1
{
get { return _selectedItem1; }
set
{
_selectedItem1 = value;
RaisePropertyChanged("SelectedItem1");
RaisePropertyChanged("SelectedItem2");
}
}
当SelectedItem1发生变化时,这将导致重新评估SelectedItem2上的验证。
答案 1 :(得分:1)
模型基础
public abstract class Model : INotifyPropertyChanged, IDataErrorInfo
{
public event PropertyChangedEventHandler PropertyChanged;
public void SetPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Error
{
get { return string.Empty; }
}
public string this[string propertyName]
{
get
{
return GetErrorForProperty(propertyName);
}
}
public abstract string GetErrorForProperty(string propertyName);
}
模型类
public class EmployeeModel : Model
{
#region Fields
private int employeeId;
private string employeeCode;
private string firstName;
private string lastName;
private DateTime? dateOfJoining;
private DateTime dob;
private string email;
private int? departmentId;
private string departmentName;
private string password;
private string role;
#endregion
#region Public Properties
public int EmployeeId
{
get
{
return employeeId;
}
set
{
if (value != this.employeeId)
{
employeeId = value;
SetPropertyChanged("EmployeeId");
}
}
}
public string EmployeeCode
{
get
{
return employeeCode;
}
set
{
if (value != this.employeeCode)
{
employeeCode = value;
SetPropertyChanged("EmployeeCode");
}
}
}
public DateTime? DateOfJoining
{
get
{
return dateOfJoining;
}
set
{
if (value != this.dateOfJoining)
{
dateOfJoining = Convert.ToDateTime(value);
SetPropertyChanged("DateofJoining");
}
}
}
public string FirstName
{
get
{
return firstName;
}
set
{
if (value != this.firstName)
{
firstName = value;
SetPropertyChanged("FirstName");
}
}
}
public string LastName
{
get
{
return lastName;
}
set
{
if (value != this.lastName)
{
lastName = value;
SetPropertyChanged("LastName");
}
}
}
public string FullName
{
get
{
return string.Join(" ", new[] { firstName, lastName });
}
}
public int? DepartmentId
{
get
{
return departmentId;
}
set
{
if (value != this.departmentId)
{
departmentId = value;
SetPropertyChanged("DepartmentId");
}
}
}
public string DepartmentName
{
get
{
return departmentName;
}
set
{
if (value != this.departmentName)
{
departmentName = value;
SetPropertyChanged("DepartmentName");
}
}
}
public DateTime DOB
{
get
{
return dob;
}
set
{
if (value != this.dob)
{
dob = Convert.ToDateTime(value);
SetPropertyChanged("DateofBirth");
}
}
}
public string Email
{
get
{
return email;
}
set
{
if (value != this.email)
{
email = value;
SetPropertyChanged("Email");
}
}
}
public string Password
{
get
{
return password;
}
set
{
if (value != this.password)
{
password = value;
SetPropertyChanged("Password");
}
}
}
public string Role
{
get
{
return role;
}
set
{
if (value != this.role)
{
role = value;
SetPropertyChanged("Role");
}
}
}
#endregion
#region Private Methods
private bool IsValid(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (Exception)
{
return false;
}
}
#endregion
#region Public Methods
public override string GetErrorForProperty(string propertyName)
{
string retErrorMsg = string.Empty;
switch (propertyName)
{
case "EmployeeCode":
if (EmployeeCode == null || EmployeeCode.Length < 2)
{
retErrorMsg = AppConstants.EmpCodeError;
}
break;
case "FirstName":
if (FirstName == null || FirstName == string.Empty)
{
retErrorMsg = AppConstants.FNameError;
}
break;
case "LastName":
if (LastName == null || LastName == string.Empty)
{
retErrorMsg = AppConstants.LNameError;
}
break;
case "DepartmentId":
if (DepartmentId == null || DepartmentId < 1)
{
retErrorMsg = AppConstants.DepartmentError;
}
break;
case "DOB":
if (DOB.AddYears(60).Date < DateTime.Now.Date || DOB.AddYears(18).Date > DateTime.Now.Date)
{
retErrorMsg = AppConstants.DOBError;
}
break;
case "DateOfJoining":
if (DateOfJoining == null || DateOfJoining > DateTime.Now)
{
retErrorMsg = AppConstants.DOJError;
}
break;
case "Role":
if (!(Role == "A" || Role == "U"))
{
retErrorMsg = AppConstants.RoleError;
}
break;
case "Email":
if (!IsValid(Email))
{
retErrorMsg = AppConstants.EmailError;
}
break;
case "Password":
if ((Password == null || Password.Length < 8))
{
retErrorMsg = AppConstants.PasswordError;
}
break;
}
return retErrorMsg;
}
#endregion
}
答案 2 :(得分:0)
并使用此基本模型转换器进行数据转换
public static class EntityConverter
{
public static void Fill(Object sourceObj, Object targetObj)
{
Type sourcetype = sourceObj.GetType();
Type targettype = targetObj.GetType();
PropertyInfo[] sourceProps = sourcetype.GetProperties();
PropertyInfo[] targetProps = targettype.GetProperties();
foreach (var sourceProperty in sourceProps)
{
try
{
var value = sourceProperty.GetValue(sourceObj);
var targetProperty = (from t in targetProps
where t.Name == sourceProperty.Name
select t).SingleOrDefault();
if (targetProperty != null)
{
if (sourceProperty.GetType() == targetProperty.GetType())
{
targetProperty.SetValue(targetObj, value);
}
}
}
catch (Exception e)
{
}
}
}
}
答案 3 :(得分:0)
public class StatusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((int)value)
{
case 1:
return "Raised";
case 2:
return "Work in Progress";
case 3:
return "Resolved";
case 4:
return "Closed";
default:
return "undefined";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (value.ToString())
{
case "Raised":
return 1;
case "Work in Progress":
return 2;
case "Resolved":
return 3;
case "Closed":
return 4;
default:
return 0;
}
}
}