다음을 통해 공유


BindingGroup.ValidationRules 속성

정의

ValidationRule에서 소스 개체의 유효성을 검사하는 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)

속성 값

Collection<ValidationRule>

ValidationRule에서 소스 개체의 유효성을 검사하는 BindingGroup 개체의 컬렉션입니다.

예제

다음 예제에서는 사용자 지정 ValidationRule, ValidateDateAndPriceBindingGroup입니다.

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

다음 예제에서는 ValidateDateAndPrice 클래스를 보여줍니다. 합니다 Validate 메서드는 BindingGroup 양식과 100 달러 개 항목을 사용 하는 경우이 수 있는 최소 7 일 동안 확인에 입력 한 값을 가져옵니다.

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

설명

경우는 ValidationRule 에 추가 됩니다을 BindingGroupBindingGroup 의 첫 번째 매개 변수로 전달 됩니다는 Validate 메서드. 사용 하 여 개체의 제안 된 값을 가져올 수는 TryGetValue 또는 GetValue(Object, String) 메서드. 바인딩 소스 개체를 가져올 수 있습니다는 Items 속성입니다.

적용 대상