ValidationStep 열거형


ValidationRule이 실행되는 시점을 지정합니다.Specifies when a ValidationRule runs.

public enum class ValidationStep
public enum ValidationStep
type ValidationStep = 
Public Enum ValidationStep


CommittedValue 3

값이 소스로 커밋된 후 ValidationRule을 실행합니다.Runs the ValidationRule after the value has been committed to the source.

ConvertedProposedValue 1

값이 변환된 후 ValidationRule을 실행합니다.Runs the ValidationRule after the value is converted.

RawProposedValue 0

변환이 발생하기 전에 ValidationRule을 실행합니다.Runs the ValidationRule before any conversion occurs.

UpdatedValue 2

소스가 업데이트된 후 ValidationRule을 실행합니다.Runs the ValidationRule after the source is updated.


다음 예에서는 이라는 유효성 검사 규칙을 사용 합니다 ValidateDateAndPrice .The following example uses a validation rule named ValidateDateAndPrice. ValidationRule.ValidationStep이 규칙에 대 한는를로 설정 하 여 ConvertedProposedValue 실행 될 때 ValidationRule.Validate 메서드는 소스 속성의 형식에 해당 하는 값에 액세스할 수 있습니다.The ValidationRule.ValidationStep for this rule is set to ConvertedProposedValue so that when it runs, the ValidationRule.Validate method has access to values that are of the source property's type.

PriceIsAPositiveNumberFutureDateRule 규칙이 실행 될 때의 기본값은 이므로 각 ValidationRule.Validate 메서드의 값은 문자열입니다 ValidationRule.ValidationStep RawProposedValue .When the PriceIsAPositiveNumber and FutureDateRule rules run, the values in each ValidationRule.Validate method are strings because the default value of ValidationRule.ValidationStep is RawProposedValue. 따라서 규칙 값은 해당 형식으로 변환 되기 전에 실행 됩니다.Therefore, the rules run before the values are converted to their respective types.

<StackPanel Name="stackPanel1"  Margin="10" Width="250"

    <Style TargetType="HeaderedContentControl">
      <Setter Property="Margin" Value="2"/>
      <Setter Property="Focusable" Value="False"/>
      <Setter Property="Template">
          <ControlTemplate TargetType="HeaderedContentControl">
            <DockPanel LastChildFill="False">
              <ContentPresenter ContentSource="Header" DockPanel.Dock="Left" Focusable="False" VerticalAlignment="Center"/>
              <ContentPresenter ContentSource="Content" Margin="5,0,0,0" DockPanel.Dock="Right" VerticalAlignment="Center"/>


    <Style TargetType="Button">
      <Setter Property="Width" Value="100"/>
      <Setter Property="Margin" Value="10,15,15,15"/>


    <BindingGroup NotifyOnValidationError="True">
        <src:ValidateDateAndPrice ValidationStep="ConvertedProposedValue" />
  <TextBlock FontSize="14" Text="Enter an item for sale"/>
  <HeaderedContentControl Header="Description">
    <TextBox Width="150" Text="{Binding Path=Description, Mode=TwoWay}"/>
  <HeaderedContentControl Header="Price">
    <TextBox Name="priceField"  Width="150">
        <Binding Path="Price" Mode="TwoWay" >
  <HeaderedContentControl Header="Date Offer Ends">
    <TextBox Name="dateField" Width="150" >
        <Binding Path="OfferExpires" StringFormat="d" >
  <StackPanel Orientation="Horizontal">
    <Button IsDefault="True" Click="Submit_Click">_Submit</Button>
    <Button IsCancel="True" Click="Cancel_Click">_Cancel</Button>
  <HeaderedContentControl Header="Description">
    <TextBlock Width="150" Text="{Binding Path=Description}"/>
  <HeaderedContentControl Header="Price">
    <TextBlock Width="150" Text="{Binding Path=Price, StringFormat=c}"/>
  <HeaderedContentControl Header="Date Offer Ends">
    <TextBlock Width="150" Text="{Binding Path=OfferExpires, StringFormat=d}"/>

다음 예에서는 ValidateDateAndPrice 유효성 검사 규칙을 보여 줍니다.The following example shows the ValidateDateAndPrice validation rule. ValidationRule.Validate메서드에서 속성은 형식이 며 Price 속성은 형식입니다 Double OfferExpires DateTime .In the ValidationRule.Validate method, the Price property is of type Double, and the OfferExpires property is of type DateTime. 문자열의 시간을 기준으로 해당 형식으로 변환 된는 ValidationRule 실행 합니다.The strings have been converted to their respective types by the time the ValidationRule runs.

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

적용 대상