Edytuj

Udostępnij za pośrednictwem


DataGrid.RowValidationRules Property

Definition

Gets the rules that are used to validate the data in each row.

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)

Property Value

The rules that are used to validate the data in each row.

Examples

The following example demonstrates a ValidationRule that checks whether the StartDate property value for a Course object is earlier than its EndDate property value. This code example is part of a larger example provided for the How to: Implement Validation with the DataGrid Control topic.

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

The following example sets the RowValidationRules property in XAML. The ValidationStep property is set to UpdatedValue so that the validation occurs only after the bound data object is updated. When a user specifies an end date that is earlier than the start date, a red exclamation mark (!) appears in the row header.

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

Remarks

The DataGrid control enables you to perform validation at both the cell and row level. With cell-level validation, you validate individual properties of a bound data object when a user updates a value. With row-level validation, you validate entire data objects when a user commits changes to a row. To create a custom validation rule, create a class that derives from the ValidationRule class and implement the Validate method. Add the custom validation rule to the RowValidationRules collection.

If the ItemBindingGroup property is used, the RowValidationRules property is ignored.

Applies to

See also