DataGrid.RowValidationRules 屬性

定義

取得規則,以用來驗證每一列的資料。

public:
 property System::Collections::ObjectModel::ObservableCollection<System::Windows::Controls::ValidationRule ^> ^ RowValidationRules { System::Collections::ObjectModel::ObservableCollection<System::Windows::Controls::ValidationRule ^> ^ get(); };
public System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.ValidationRule> RowValidationRules { get; }
member this.RowValidationRules : System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.ValidationRule>
Public ReadOnly Property RowValidationRules As ObservableCollection(Of ValidationRule)

屬性值

規則,用來驗證每個資料列中的資料。

範例

下列範例示範的 ValidationRule ,會檢查 物件的屬性值 Course 是否 StartDate 早于其 EndDate 屬性值。 此程式碼範例是 如何:使用 DataGrid 控制項實作驗證 主題所提供的較大範例的一部分。

public class CourseValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value,
        System.Globalization.CultureInfo cultureInfo)
    {
        Course course = (value as BindingGroup).Items[0] as Course;
        if (course.StartDate > course.EndDate)
        {
            return new ValidationResult(false,
                "Start Date must be earlier than End Date.");
        }
        else
        {
            return ValidationResult.ValidResult;
        }
    }
}
Public Class CourseValidationRule
    Inherits ValidationRule

    Public Overrides Function Validate(ByVal value As Object, _
        ByVal cultureInfo As System.Globalization.CultureInfo) _
        As ValidationResult

        Dim course As Course = _
            CType(CType(value, BindingGroup).Items(0), Course)

        If course.StartDate > course.EndDate Then
            Return New ValidationResult(False, _
                "Start Date must be earlier than End Date.")
        Else
            Return ValidationResult.ValidResult
        End If

    End Function

End Class

下列範例會在 RowValidationRules XAML 中設定 屬性。 屬性 ValidationStep 設定 UpdatedValue 為 ,因此只有在更新系結的資料物件之後,才會進行驗證。 當使用者指定早于開始日期的結束日期時,資料列標頭中會出現紅色驚嘆號 (!) 。

<DataGrid.RowValidationRules>
  <local:CourseValidationRule ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>

備註

控制項 DataGrid 可讓您在儲存格和資料列層級執行驗證。 使用資料格層級驗證,您可以在使用者更新值時驗證系結資料物件的個別屬性。 透過資料列層級驗證,您可以在使用者認可變更至資料列時驗證整個資料物件。 若要建立自訂驗證規則,請建立衍生自 類別的 ValidationRule 類別,並實作 Validate 方法。 將自訂驗證規則新增至 RowValidationRules 集合。

ItemBindingGroup如果使用 屬性,則會 RowValidationRules 忽略 屬性。

適用於

另請參閱