Export (0) Print
Expand All

ValidationRule.ValidationStep Property

Gets or sets when the validation rule runs.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

'Declaration
Public Property ValidationStep As ValidationStep
<object ValidationStep="ValidationStep" .../>

Property Value

Type: System.Windows.Controls.ValidationStep
One of the enumeration values. The default is ValidationStep.RawProposedValue.

The following example sets the ValidationStep to ConvertedProposedValue on the ValidationRule called ValidateDateAndPrice so that when it runs, the Validate method has access to values that are of the source property's type. When the rules PriceIsAPositiveNumber and FutureDateRule run, the values in each Validate method are strings because 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>

The following example shows the ValidationRule ValidateDateAndPrice. In the Validate method override, the Price property is of type Double and the OfferExpires property is of type DateTime because the strings have been converted to their respective types by the time the ValidationRule runs.

	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

.NET Framework

Supported in: 4.5, 4, 3.5 SP1, 3.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft