Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

ErrorControl Class

The validation error.

Namespace: DevExpress.Xpf.Editors

Assembly: DevExpress.Xpf.Core.v24.2.dll

NuGet Package: DevExpress.Wpf.Core

#Declaration

public class ErrorControl :
    ContentControl

#Remarks

The ErrorControl displays an icon in a data editor when input validation fails.

#Example

This example uses the ErrorControl to display icons within the text editor if the user entered the invalid text. The data object (TestData) implements the IDataErrorInfo interface.

WPF Error Control, DevExpress

View Example

<Window x:Class="WpfApplication147.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350"
        Width="525" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxet="http://schemas.devexpress.com/winfx/2008/xaml/editors/themekeys"
        xmlns:local="clr-namespace:WpfApplication147">
    <Window.Resources>
        <ResourceDictionary>
            <local:ErrorContentConverter x:Key="ErrorContentToErrorTypeConverter" GetValueTag="ErrorType" Separator=";"/>
            <local:ErrorContentConverter x:Key="ErrorContentConverter" GetValueTag="ErrorContent" Separator=";"/>
            <Style TargetType="{x:Type dxe:ErrorControl}" BasedOn="{StaticResource {x:Type dxe:ErrorControl}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Content.ErrorContent, RelativeSource={RelativeSource Self}, Converter={StaticResource ErrorContentToErrorTypeConverter}}" Value="Critical">
                        <Setter Property="ContentTemplate" Value="{DynamicResource {dxet:ErrorTypesThemeKeyExtension ResourceKey=Critical}}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Content.ErrorContent, RelativeSource={RelativeSource Self}, Converter={StaticResource ErrorContentToErrorTypeConverter}}" Value="Information">
                        <Setter Property="ContentTemplate" Value="{DynamicResource {dxet:ErrorTypesThemeKeyExtension ResourceKey=Information}}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ResourceDictionary>
    </Window.Resources>

    <Window.DataContext>
        <local:TestClass TestString="test"/>
    </Window.DataContext>

    <StackPanel>
        <dxe:TextEdit EditValue="{Binding Path=TestString, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}">
            <dxe:TextEdit.ErrorToolTipContentTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=ErrorContent, Converter={StaticResource ErrorContentConverter}}" />
                </DataTemplate>
            </dxe:TextEdit.ErrorToolTipContentTemplate>
        </dxe:TextEdit>
    </StackPanel>
</Window>
public class TestClass : IDataErrorInfo, INotifyPropertyChanged {
    string testString;
    public string TestString {
        get { return testString; }
        set {
            testString = value;
            RaisePropertyChanged("TestString");
        }
    }

    #region IDataErrorInfo Members

    string IDataErrorInfo.Error {
        get { return GetError(); }
    }
    string GetError() {
        if (string.IsNullOrEmpty(TestString))
            return "ErrorType=Critical;ErrorContent=The value cannot be empty.";
        if (TestString.Length < 3)
            return "ErrorType=Critical;ErrorContent=The value must be at least 3 characters long.";
        if (TestString.Length < 5)
            return "ErrorType=Information;ErrorContent=The value is short but acceptable.";
        return string.Empty;
    }
    string IDataErrorInfo.this[string columnName] {
        get {
            if (columnName == "TestString")
                return GetError();
            return string.Empty;
        }
    }

    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged(string property) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    #endregion
}
public class ErrorContentConverter : IValueConverter {
    public string GetValueTag { get; set; }
    public string Separator { get; set; }

    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        if (value == null || !(value is string))
            return value;
        string error = System.Convert.ToString(value, culture);
        if (string.IsNullOrEmpty(error))
            return value;

        string searchString = GetValueTag + "=";
        foreach (string suberror in error.Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries)) {
            if (suberror.Contains(searchString))
                return suberror.Replace(searchString, string.Empty);
        }
        return value;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return null;
    }

    #endregion
}
See Also