Back to Devexpress

DocumentPreviewControl.ValidateEditingField Event

wpf-devexpress-dot-xpf-dot-printing-dot-documentpreviewcontrol-d3120d09.md

latest8.9 KB
Original Source

DocumentPreviewControl.ValidateEditingField Event

Occurs before the editor invoked for the edit field is closed. Allows you to validate the edit field value.

Namespace : DevExpress.Xpf.Printing

Assembly : DevExpress.Xpf.Printing.v25.2.dll

NuGet Package : DevExpress.Wpf.Printing

Declaration

csharp
public event ValidateEditingFieldEventHandler ValidateEditingField
vb
Public Event ValidateEditingField As ValidateEditingFieldEventHandler

Event Data

The ValidateEditingField event's data class is EditingFieldValidationEventArgs. The following properties provide information specific to this event:

PropertyDescription
EditingFieldGets the edit field for which the event is raised. Inherited from EditingFieldEditorEventArgsBase.
ErrorMessageSpecifies the tooltip message.
ErrorTypeSpecifies the type of the error icon displayed in the editor.
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
IsValidSpecifies whether the edit field value is valid.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
ValueGets the edit field value.

The event data class exposes the following methods:

MethodDescription
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

The following code validates the value in the edit field’s Date Time Editor so that the user can enter only dates for the current month:

xaml
<Window x:Class="ValidateEditingFields_MVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ValidateEditingFields_MVVM"
        xmlns:dxp="http://schemas.devexpress.com/winfx/2008/xaml/printing"
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid>
        <dxp:DocumentPreviewControl RequestDocumentCreation="True"
                                    DocumentSource="{Binding Report}"
                                    HighlightEditingFields="True">
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:EventToCommand Event="{x:Static dxp:DocumentPreviewControl.ValidateEditingFieldEvent}"
                                       PassEventArgsToCommand="True"
                                       Command="{Binding OnValidateEditingFieldCommand}" />
            </dxmvvm:Interaction.Behaviors>
        </dxp:DocumentPreviewControl>
    </Grid>
</Window>
csharp
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Xpf.Printing;
using DevExpress.XtraReports.UI;
using System;

namespace ValidateEditingFields_MVVM {
    public class ViewModel : ViewModelBase {
        public XtraReport Report { get; }
        public ViewModel() {
            Report = new XtraReport1();
        }
        [Command]
        public void OnValidateEditingField(EditingFieldValidationEventArgs args) {
            if(args.EditingField.ID == "DateField")
                ValidateDateTimeEditingField(args);
        }

        void ValidateDateTimeEditingField(EditingFieldValidationEventArgs args) {
            DateTime value = default(DateTime);
            try {
                value = Convert.ToDateTime(args.Value);
            } catch (Exception e) {
                args.IsValid = false;
                args.ErrorMessage = e.Message;
                args.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
            }

            DateTime now = DateTime.Now;
            args.IsValid = value.Year == now.Year && value.Month == now.Month;
        }
    }
}
vb
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Xpf.Printing
Imports DevExpress.XtraReports.UI
Imports System

Namespace ValidateEditingFields_MVVM
    Public Class ViewModel
        Inherits ViewModelBase

        Public ReadOnly Property Report() As XtraReport
        Public Sub New()
            Report = New XtraReport1()
        End Sub
        <Command>
        Public Sub OnValidateEditingField(ByVal args As EditingFieldValidationEventArgs)
            If args.EditingField.ID = "DateField" Then
                ValidateDateTimeEditingField(args)
            End If
        End Sub

        Private Sub ValidateDateTimeEditingField(ByVal args As EditingFieldValidationEventArgs)
            Dim value As Date = Date.MinValue
            Try
                value = Convert.ToDateTime(args.Value)
            Catch e As Exception
                args.IsValid = False
                args.ErrorMessage = e.Message
                args.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical
            End Try

            Dim now As Date = Date.Now
            args.IsValid = value.Year = now.Year AndAlso value.Month = now.Month
        End Sub
    End Class
End Namespace

View Example: How to Validate the Editing Field Value in the Document Preview

See Also

DocumentPreviewControl Class

DocumentPreviewControl Members

DevExpress.Xpf.Printing Namespace