BindingExpressionBase.ValidateWithoutUpdate Method ()

.NET Framework (current version)

Runs any ValidationRule objects on the associated Binding that have the ValidationStep property set to RawProposedValue or ConvertedProposedValue. This method does not update the source.

Namespace:   System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)

Public Function ValidateWithoutUpdate As Boolean

Return Value

Type: System.Boolean

true if the validation rules succeed; otherwise, false.

The ValidateWithoutUpdate method enables you to run validation rules on a binding without updating the source of the binding. This is useful when you want to validate user input and update the source at different times in an application.

For example, suppose you have a form to update a data source that contains a submit button. You want to provide feedback to the user if an invalid value is entered before the user attempts to submit the form. You can check the validity of a field by setting the binding's UpdateSourceTrigger property to Explicit and calling ValidateWithoutUpdate when the TextBox loses focus.

The following example demonstrates the scenario that is discussed in the Remarks section. The example enables a user to enter data for a library item and submit the data. The binding for the TextBox named callNum has a ValidationRule associated with it. When the TextBox loses focus, the example calls ValidateWithoutUpdate. When the user clicks the submit button, the example calls UpdateSource to commit the changes. The following XAML creates the user interface for the application.

        <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"/>


    <HeaderedContentControl Header="Title">
        <TextBox Width="150" Text="{Binding Path=Title, Mode=TwoWay}"/>
    <HeaderedContentControl Header="Due Date">
        <TextBox Width="150" Text="{Binding Path=DueDate, StringFormat=d, Mode=TwoWay}">


    <HeaderedContentControl Header="Call Number">
        <TextBox Name="CallNum" Width="150"
                <Binding xmlns:src="clr-namespace:BindingExpressionBaseValidateWithoutUpdating"
                    Path="CallNumber" Mode="TwoWay"
                        <src:CallNumberRule ValidationStep="ConvertedProposedValue"/>

    <Button Click="Button_Click">Submit</Button>

    <HeaderedContentControl Header="Title">
        <TextBlock Text="{Binding Path=Title, Mode=TwoWay}"/>

    <HeaderedContentControl Header="Due Date">
        <TextBlock Text="{Binding Path=DueDate, StringFormat=d, Mode=TwoWay}"/>

    <HeaderedContentControl Header="Call Number">
        <TextBlock Text="{Binding CallNumber}"/>


The following example shows the logic for the user interface.

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data

Partial Public Class Window1
    Inherits Window
    Public Sub New()

        ' Create an object and set it to the window's DataContext.
        Dim item As New LibraryItem("Enter the title", "Enter the call number", DateTime.Now + New TimeSpan(14, 0, 0, 0))

        Me.DataContext = item
    End Sub

    ' Check whether the call number is valid when the
    ' TextBox loses foces.
    Private Sub CallNum_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim be As BindingExpression = CallNum.GetBindingExpression(TextBox.TextProperty)

    End Sub

    ' Show the validation error when one occurs.
    Private Sub CallNum_Error(ByVal sender As Object, ByVal e As ValidationErrorEventArgs)
        If e.Action = ValidationErrorEventAction.Added Then

        End If
    End Sub

    ' Update the source data object when the user clicks
    ' the submit button.
    Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim be As BindingExpression = CallNum.GetBindingExpression(TextBox.TextProperty)

    End Sub
End Class

The following example shows the data object and ValidationRule for the application.

Imports System
Imports System.ComponentModel
Imports System.Windows.Controls
Imports System.Globalization

' LibraryItem implements INotifyPropertyChanged so that the 
' application is notified when a property changes. It 
' implements IEditableObject so that pending changes can be discarded.
' In this example, the application does not discard changes.
Public Class LibraryItem
    Implements INotifyPropertyChanged
    Implements IEditableObject
    Private Structure ItemData
        Friend Title As String
        Friend CallNumber As String
        Friend DueDate As DateTime
    End Structure

    Private copyData As ItemData
    Private currentData As ItemData

    Public Sub New(ByVal title As String, ByVal callNum As String, ByVal dueDate As DateTime)
        Me.Title = title
        Me.CallNumber = callNum
        Me.DueDate = DueDate
    End Sub

    Public Property Title() As String
            Return currentData.Title
        End Get
        Set(ByVal value As String)
            If currentData.Title <> value Then
                currentData.Title = value
            End If
        End Set
    End Property

    Public Property CallNumber() As String
            Return currentData.CallNumber
        End Get
        Set(ByVal value As String)
            If currentData.CallNumber <> value Then
                currentData.CallNumber = value
            End If
        End Set
    End Property

    Public Property DueDate() As DateTime
            Return currentData.DueDate
        End Get
        Set(ByVal value As DateTime)
            If value <> currentData.DueDate Then
                currentData.DueDate = value
            End If
        End Set
    End Property

#Region "INotifyPropertyChanged Members"

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub NotifyPropertyChanged(ByVal info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

#End Region

#Region "IEditableObject Members"

    Public Overridable Sub BeginEdit() _
        Implements IEditableObject.BeginEdit

        copyData = currentData
    End Sub

    Public Overridable Sub CancelEdit() _
        Implements IEditableObject.CancelEdit

        currentData = copyData

    End Sub

    Public Overridable Sub EndEdit() _
        Implements IEditableObject.EndEdit

        copyData = New ItemData()
    End Sub

#End Region

End Class

Public Class CallNumberRule
    Inherits ValidationRule

    ' A valid call number contains a period (.)
    ' and 6 characters after the period.
    Public Overloads Overrides Function Validate(ByVal value As Object,
                               ByVal cultureInfo As CultureInfo) As ValidationResult

        Dim callNum As String = DirectCast(value, String)

        Dim dotIndex As Integer = callNum.IndexOf(".")

        If dotIndex = -1 OrElse dotIndex = 0 Then
            Return New ValidationResult(False,
                "There must be characters followed by a period (.) in the call number.")
        End If

        Dim substr As String = callNum.Substring(dotIndex + 1)

        If substr.Length <> 6 Then
            Return New ValidationResult(False,
                "The call number must have 6 characters after the period (.).")
        End If

        Return ValidationResult.ValidResult
    End Function
End Class

.NET Framework
Available since 4.0
Return to top