2017-03-29 73 views
0

我想驗證DataGridCell基於其他DataGridRows在同一DataGrid的值。但在我繼承的MyValidationRule對象中,我無法訪問DataGrid的所有綁定項目,但只能訪問當前行。DataGrid行驗證基於其他行

如何在ValidationRuleValidate()方法中訪問DataGrid的其他綁定項?

回答

0

你想要做什麼,我認爲是按照視覺樹高達DataGrid中,並從那裏訪問的ItemsSource來進行驗證。像這樣的...

public class SampleRowValidationRule : ValidationRule 
{ 
    public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
    { 
     BindingGroup bg = value as BindingGroup; 
     if (bg != null && bg.Items.Count > 0) 
     { 
      DataGridRow dgrow = bg.Owner as DataGridRow; 
      if(dgrow != null) 
      { 
       DataGrid dg = GetParent<DataGrid>(dgrow); 
       // ... add more 
      } 
     } 
     return new ValidationResult(true, null); 
    } 
    private T GetParent<T>(DependencyObject d) where T : class 
    { 
     while (d != null && !(d is T)) 
     { 
      d = VisualTreeHelper.GetParent(d); 
     } 
     return d as T; 

    } 
}