A newer version of this page is available. Switch to the current version.

IDataErrorInfoHelper Class

Provides extension methods to get an error based on defined DataAnnotation attributes or Fluent API

Namespace: DevExpress.Mvvm

Assembly: DevExpress.Mvvm.v20.2.dll

Declaration

public static class IDataErrorInfoHelper

Remarks

The following code sample enables validation for TextEdit controls. If they contain valid text, the Save button is enabled:

<UserControl 
    xmlns:ViewModels="clr-namespace:Example.ViewModels"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <UserControl.Resources>
        <dxmvvm:BooleanNegationConverter x:Key="BooleanNegationConverter"/>
    </UserControl.Resources>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:ValidationErrorsHostBehavior x:Name="validationErrorsHostBehavior"/>
    </dxmvvm:Interaction.Behaviors>
    <Grid>
        <StackPanel Orientation="Vertical" ...>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="First Name: " .../>
                <dxe:TextEdit Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
                                NotifyOnValidationError=True, ValidatesOnDataErrors=True}" .../>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Last Name: " .../>
                <dxe:TextEdit Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
                                NotifyOnValidationError=True, ValidatesOnDataErrors=True}" .../>
            </StackPanel>
            <Button Content="Login" ... IsEnabled="{Binding ElementName=validationErrorsHostBehavior, 
                                                   Path=HasErrors, Converter={StaticResource BooleanNegationConverter}}"/>
        </StackPanel>
    </Grid>
</UserControl>
using DevExpress.Mvvm;
using System.ComponentModel;

namespace Example.ViewModel {
    public class MainViewModel : ViewModelBase, IDataErrorInfo {
        public string FirstName {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }

        public string LastName {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }

        public string Error {
            get {
                return this["FirstName"] != null || this["LastName"] != null ? "Invalid values." : null;
            }
        }

        public string this[string columnName] {
            get {
                switch (columnName) {
                    case "FirstName":
                        return string.IsNullOrEmpty(FirstName) ? "First Name cannot be empty." : null;
                    case "LastName":
                        return string.IsNullOrEmpty(LastName) ? "Last Name cannot be empty." : null;
                    default:
                        return null;
                }

            }
        }
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the IDataErrorInfoHelper class.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

Inheritance

Object
IDataErrorInfoHelper
See Also