Back to Devexpress

ErrorControl Class

wpf-devexpress-dot-xpf-dot-editors-de38ed39.md

latest11.1 KB
Original Source

ErrorControl Class

The validation error.

Namespace : DevExpress.Xpf.Editors

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

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public class ErrorControl :
    ContentControl
vb
Public Class ErrorControl
    Inherits 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.

View Example

xaml
<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>
csharp
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
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports DevExpress.Xpf.Editors
Imports System.ComponentModel

Namespace WpfApplication147
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
        End Sub

    End Class

    Public Class TestClass
        Implements IDataErrorInfo, INotifyPropertyChanged
        Private testString_Renamed As String
        Public Property TestString() As String
            Get
                Return testString_Renamed
            End Get
            Set(ByVal value As String)
                testString_Renamed = value
                RaisePropertyChanged("TestString")
            End Set
        End Property

        #Region "INotifyPropertyChanged Members"

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Private Sub RaisePropertyChanged(ByVal [property] As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs([property]))
        End Sub

        #End Region

        #Region "IDataErrorInfo Members"

        Private ReadOnly Property IDataErrorInfo_Error() As String Implements IDataErrorInfo.Error
            Get
                Return GetError()
            End Get
        End Property
        Private Function GetError() As String
            If String.IsNullOrEmpty(TestString) Then
                Return "ErrorType=Critical;ErrorContent=empty"
            End If
            If TestString.Length < 3 Then
                Return "ErrorType=Critical;ErrorContent=error"
            End If
            If TestString.Length < 5 Then
                Return "ErrorType=Information;ErrorContent=warning"
            End If
            Return String.Empty
        End Function
        Public ReadOnly Property IDataErrorInfo_Item(ByVal columnName As String) As String Implements IDataErrorInfo.Item
            Get
                If columnName = "TestString" Then
                    Return GetError()
                End If
                Return String.Empty
            End Get
        End Property

        #End Region
    End Class
    Public Class ErrorContentConverter
        Implements IValueConverter
        Private privateGetValueTag As String
        Public Property GetValueTag() As String
            Get
                Return privateGetValueTag
            End Get
            Set(ByVal value As String)
                privateGetValueTag = value
            End Set
        End Property
        Private privateSeparator As String
        Public Property Separator() As String
            Get
                Return privateSeparator
            End Get
            Set(ByVal value As String)
                privateSeparator = value
            End Set
        End Property

        #Region "IValueConverter Members"
        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            If value Is Nothing OrElse Not(TypeOf value Is String) Then
                Return value
            End If
            Dim [error] As String = System.Convert.ToString(value, culture)
            If String.IsNullOrEmpty([error]) Then
                Return value
            End If

            Dim searchString As String = GetValueTag & "="
            For Each suberror As String In [error].Split(New String() { Separator }, StringSplitOptions.RemoveEmptyEntries)
                If suberror.Contains(searchString) Then
                    Return suberror.Replace(searchString, String.Empty)
                End If
            Next suberror
            Return value
        End Function
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return Nothing
        End Function

        #End Region
    End Class
End Namespace

Inheritance

Object DispatcherObject DependencyObject Visual UIElement FrameworkElement Control ContentControl ErrorControl

See Also

ErrorControl Members

DevExpress.Xpf.Editors Namespace