wpf-devexpress-dot-xpf-dot-printing-dot-documentpreviewcontrol-d3120d09.md
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
public event ValidateEditingFieldEventHandler ValidateEditingField
Public Event ValidateEditingField As ValidateEditingFieldEventHandler
The ValidateEditingField event's data class is EditingFieldValidationEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| EditingField | Gets the edit field for which the event is raised. Inherited from EditingFieldEditorEventArgsBase. |
| ErrorMessage | Specifies the tooltip message. |
| ErrorType | Specifies the type of the error icon displayed in the editor. |
| Handled | Gets 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. |
| IsValid | Specifies whether the edit field value is valid. |
| OriginalSource | Gets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs. |
| RoutedEvent | Gets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs. |
| Source | Gets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs. |
| Value | Gets the edit field value. |
The event data class exposes the following methods:
| Method | Description |
|---|---|
| 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. |
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:
<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>
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;
}
}
}
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