BindingGroup.ValidationRules Eigenschaft

Definition

Ruft eine Auflistung von ValidationRule-Objekten ab, die die Quellobjekte in der BindingGroup überprüfen.Gets a collection of ValidationRule objects that validate the source objects in the 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)

Eigenschaftswert

Collection<ValidationRule>

Eine Auflistung von ValidationRule-Objekten, die die Quellobjekte in der BindingGroup überprüfen.A collection of ValidationRule objects that validate the source objects in the BindingGroup.

Beispiele

Im folgenden Beispiel wird der benutzerdefinierte,, der hinzugefügt ValidationRule ValidateDateAndPrice BindingGroup .The following example adds the custom ValidationRule, ValidateDateAndPrice, to the BindingGroup.

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

Im folgenden Beispiel wird die ValidateDateAndPrice-Klasse gezeigt.The following example shows the ValidateDateAndPrice class. Die Validate -Methode verwendet, BindingGroup um die Werte zu erhalten, die der Benutzer in das Formular eingegeben hat, und überprüft, ob ein Element, das mehr als 100 Dollar beträgt, mindestens sieben Tage lang verfügbar ist.The Validate method uses the BindingGroup to get the values the user entered into the form, and checks that if an item is over 100 dollars, it will be available for at least seven days.

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

Hinweise

Wenn ein einem ValidationRule hinzugefügt wird BindingGroup , BindingGroup wird der als erster Parameter der-Methode übergeben Validate .When a ValidationRule is added to a BindingGroup, the BindingGroup is passed in as the first parameter of the Validate method. Sie können die vorgeschlagenen Werte des-Objekts mit der- TryGetValue Methode oder der- GetValue(Object, String) Methode erhalten.You can get the proposed values of the object by using the TryGetValue or GetValue(Object, String) method. Sie können die Objekte, die die Quellen der Bindungen sind, aus der- Items Eigenschaft erhalten.You can get the objects that are the sources of the bindings from the Items property.

Gilt für: