Partager via


BindingGroup.ValidationRules Propriété

Définition

Obtient une collection d'objets ValidationRule qui valident les objets sources dans BindingGroup.

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

Valeur de propriété

Collection<ValidationRule>

Collection d'objets ValidationRule qui valident les objets sources dans BindingGroup.

Exemples

L’exemple suivant ajoute le code personnalisé ValidationRule, ValidateDateAndPriceau BindingGroup.

<StackPanel.BindingGroup>
  <BindingGroup NotifyOnValidationError="True">
    <BindingGroup.ValidationRules>
      <src:ValidateDateAndPrice ValidationStep="ConvertedProposedValue" />
    </BindingGroup.ValidationRules>
  </BindingGroup>
</StackPanel.BindingGroup>

L’exemple suivant montre la classe ValidateDateAndPrice. La Validate méthode utilise la BindingGroup méthode pour obtenir les valeurs que l’utilisateur a entrées dans le formulaire et vérifie que si un article est supérieur à 100 dollars, il sera disponible pendant au moins sept jours.

public class ValidateDateAndPrice : ValidationRule
{
    // Ensure that an item over $100 is available for at least 7 days.
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        BindingGroup bg = value as BindingGroup;

        // Get the source object.
        PurchaseItem item = bg.Items[0] as PurchaseItem;
        
        object doubleValue;
        object dateTimeValue;

        // Get the proposed values for Price and OfferExpires.
        bool priceResult = bg.TryGetValue(item, "Price", out doubleValue);
        bool dateResult = bg.TryGetValue(item, "OfferExpires", out dateTimeValue);

        if (!priceResult || !dateResult)
        {
            return new ValidationResult(false, "Properties not found");
        }

        double price = (double)doubleValue;
        DateTime offerExpires = (DateTime)dateTimeValue;

        // Check that an item over $100 is available for at least 7 days.
        if (price > 100)
        {
            if (offerExpires < DateTime.Today + new TimeSpan(7, 0, 0, 0))
            {
                return new ValidationResult(false, "Items over $100 must be available for at least 7 days.");
            }
        }

        return ValidationResult.ValidResult;
    }
}
Public Class ValidateDateAndPrice
    Inherits ValidationRule
    ' Ensure that an item over $100 is available for at least 7 days.
    Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As CultureInfo) As ValidationResult
        Dim bg As BindingGroup = TryCast(value, BindingGroup)

        ' Get the source object.
        Dim item As PurchaseItem = TryCast(bg.Items(0), PurchaseItem)

        Dim doubleValue As Object = Nothing
        Dim dateTimeValue As Object = Nothing

        ' Get the proposed values for Price and OfferExpires.
        Dim priceResult As Boolean = bg.TryGetValue(item, "Price", doubleValue)
        Dim dateResult As Boolean = bg.TryGetValue(item, "OfferExpires", dateTimeValue)

        If (Not priceResult) OrElse (Not dateResult) Then
            Return New ValidationResult(False, "Properties not found")
        End If

        Dim price As Double = CDbl(doubleValue)
        Dim offerExpires As Date = CDate(dateTimeValue)

        ' Check that an item over $100 is available for at least 7 days.
        If price > 100 Then
            If offerExpires < Date.Today + New TimeSpan(7, 0, 0, 0) Then
                Return New ValidationResult(False, "Items over $100 must be available for at least 7 days.")
            End If
        End If

        Return ValidationResult.ValidResult

    End Function
End Class

Remarques

Lorsqu’un ValidationRule élément est ajouté à un BindingGroup, il BindingGroup est passé en tant que premier paramètre de la Validate méthode. Vous pouvez obtenir les valeurs proposées de l’objet à l’aide de la méthode ou GetValue(Object, String) de la TryGetValue méthode. Vous pouvez obtenir les objets qui sont les sources des liaisons à partir de la Items propriété.

S’applique à