ValidationStep ValidationStep ValidationStep ValidationStep Enum

Définition

Spécifie à quel moment un ValidationRule s'exécute.Specifies when a ValidationRule runs.

public enum class ValidationStep
public enum ValidationStep
type ValidationStep = 
Public Enum ValidationStep
Héritage
ValidationStepValidationStepValidationStepValidationStep

Champs

CommittedValue CommittedValue CommittedValue CommittedValue 3

Exécute le ValidationRule une fois la valeur validée dans la source.Runs the ValidationRule after the value has been committed to the source.

ConvertedProposedValue ConvertedProposedValue ConvertedProposedValue ConvertedProposedValue 1

Exécute le ValidationRule une fois la valeur convertie.Runs the ValidationRule after the value is converted.

RawProposedValue RawProposedValue RawProposedValue RawProposedValue 0

Exécute le ValidationRule avant toute conversion.Runs the ValidationRule before any conversion occurs.

UpdatedValue UpdatedValue UpdatedValue UpdatedValue 2

Exécute le ValidationRule une fois la source mise à jour.Runs the ValidationRule after the source is updated.

Exemples

L’exemple suivant utilise une règle de validation ValidateDateAndPricenommée.The following example uses a validation rule named ValidateDateAndPrice. La ValidationRule.ValidationStep valeur de cette règle est définie ConvertedProposedValue sur de sorte que lorsqu’elle s' ValidationRule.Validate exécute, la méthode a accès aux valeurs qui sont du type de la propriété source.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.

Lorsque les PriceIsAPositiveNumber règles FutureDateRule et s’exécutent, les valeurs ValidationRule.Validate de chaque méthode sont des chaînes, car ValidationRule.ValidationStep la RawProposedValuevaleur par défaut de est.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. Par conséquent, les règles s’exécutent avant que les valeurs soient converties en leurs types respectifs.Therefore, the rules run before the values are converted to their respective types.

<StackPanel Name="stackPanel1"  Margin="10" Width="250"
            Loaded="stackPanel1_Loaded"
            Validation.Error="ItemError">

  <StackPanel.Resources>
    <Style TargetType="HeaderedContentControl">
      <Setter Property="Margin" Value="2"/>
      <Setter Property="Focusable" Value="False"/>
      <Setter Property="Template">
        <Setter.Value>
          <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"/>
            </DockPanel>

          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

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

    </Style>

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

L’exemple suivant montre la ValidateDateAndPrice règle de validation.The following example shows the ValidateDateAndPrice validation rule. Dans la ValidationRule.Validate méthode, la Price propriété est de type Doubleet la OfferExpires propriété est de type DateTime.In the ValidationRule.Validate method, the Price property is of type Double, and the OfferExpires property is of type DateTime. Les chaînes ont été converties en leurs types respectifs au moment ValidationRule de l’exécution de.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

S’applique à