2012-05-01 7 views
0

はサンプル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; }   
} 

です。ここに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> 

この問題を解決するための良い方法です。

答えて

2

私はあなたがする必要があるすべてはSelectedItem1が変更されたとき、これはSelectedItem2の検証の再評価の原因となります

public Person SelectedItem1 
{ 
    get { return _selectedItem1; } 
    set 
    { 
     _selectedItem1 = value; 
     RaisePropertyChanged("SelectedItem1"); 
     RaisePropertyChanged("SelectedItem2"); 
    } 
} 

だと思います。

+0

はい、それはすでに私がやっていることです。私はこれを行うための他の方法はないと思う。 – user1340587

-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); 
} 

Modelクラス

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 
} 
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) 
      { 

      } 
     } 

    } 
} 
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; 
     } 
    } 
}