2011-07-06 9 views
3

私は、Employeeオブジェクトをリストにバインドされwchichデータグリッドを持つ単純なWPFアプリケーション作られた:あなたが見ることができるようにWPFデータグリッドの新しい行の検証

public class Employee 
{ 
    private string _name; 

    public int Id { get; set; } 


    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      if (String.IsNullOrEmpty(value)) 
       throw new ApplicationException("Name cannot be empty. Please specify the name."); 
      _name = value; 
     } 
    } 

を、私は設定Nameプロパティなしで従業員を作成しないようにしたいです。

public class StringValidationRule : ValidationRule 
{ 
    public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
    { 
     string str = value as string; 
     if (String.IsNullOrEmpty(str)) 
      return new ValidationResult(false, "This field cannot be empty"); 
     else 
      return new ValidationResult(true, null); 
    } 
} 

名前フィールドのためのXAMLは以下の通りです: だから、私は、検証ルールを作っ

<DataGridTextColumn Header="Name" 
           ElementStyle="{StaticResource datagridElStyle}" > 
       <DataGridTextColumn.Binding> 
        <Binding Path="Name" Mode="TwoWay" NotifyOnValidationError="True" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged" > 
         <Binding.ValidationRules> 
          <emp:StringValidationRule/> 
         </Binding.ValidationRules> 
        </Binding> 
       </DataGridTextColumn.Binding> 
      </DataGridTextColumn> 

私はDataGrid内の既存の従業員行の名前を編集して空にし、それを設定しよう文字列の場合、datagridは間違ったフィールドをマークし、行を保存することはできません。これは正しい動作です。

しかし、新しい行を作成してキーボードでEnterキーを押すと、_nameがNULLに設定されたこの新しい行が作成され、検証は機能しません。私はDataGridが新しい行オブジェクトのデフォルトのコンストラクタを呼び出し、_nameフィールドをNULLに設定するためだと思います。

新しい行の正しい検証方法は何ですか?

答えて

2

EmployeeオブジェクトにIDataErrorを実装できます。このhereには良いページがあります。

+0

THanks、私はそれを調べ、解決策があればここに投稿します。しかし、編集の検証はうまく動作し、新しい行を検証する簡単な方法があると思っていました。これはかなり一般的な作業です。 – MyUserName

+0

はい、EmployeeクラスのIDataErrorの実装が助けになりました。私はここで良い例を見つけました:http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx#errorinfo – MyUserName

+0

@MyUserName:喜んで助けてください。 :) –

0

私は実際に同じ問題を抱えていましたが、私はここでKarl Shifflett:http://bit.ly/18NCpJUのようにMVCデータ注釈を使用していたためです。私は当初考えていましたが、MVCデータアノテーションを含まないようにしたのは、フォームを送信するのに適しているように見えますが、データが存続し、編集できるアプリケーションではないためです。セッションは終了しましたが、私は脱退します。

ここで私は一時的な対処方法を行っています。長期的な解決策は、IDataErrorを実装することです:

// BusinessEntityBase is what allows us to 
// use MVC Data Annotations (http://bit.ly/18NCpJU) 
public class MyModel : BusinessEntityBase 
{ 
    private string _name; 
    private List<Action> _validationQueue = new List<Action>(); 
    private Timer _timer = new Timer { Interval = 500 }; 

    [Required(AllowEmptyStrings = false)] 
    public string Name 
    { 
     get 
     { 
      return this._name; 
     } 
     set 
     { 
      var currentValue = this._name; 
      this._name = value; 
      base.RaisePropertyChanged("Name"); 

      this.AddValidationAction("Name", currentValue, value ); 
     } 
    } 

    private void AddValidationAction<T>(string Name, T currentValue, T newValue) 
    { 
     Action validationAction = 
      () => 
       base.SetPropertyValue(Name, ref currentValue, newValue); 
     _validationQueue.Add(validationAction); 
     this._timer.Enabled = true; 
    } 

    private void ProcessValidationQueue(object sender, ElapsedEventArgs e) 
    { 
     if(_validationQueue.Count > 0) 
     { 
      while (_validationQueue.Count > 0) 
      { 
       _validationQueue[0].Invoke(); 
       _validationQueue.RemoveAt(0); 
      } 
     } 

     this._timer.Enabled = false; 
    } 

    public MyModel() 
    { 
     _timer.Enabled = false; 
     _timer.Elapsed += this.ProcessValidationQueue; 
     _timer.Start(); 
    } 
} 
関連する問題