ErrorControl Class
Namespace: DevExpress.Xpf.Editors
Assembly:
DevExpress.Xpf.Core.v24.1.dll
NuGet Package:
DevExpress.Wpf.Core
Declaration
public class ErrorControl :
ContentControl
Public Class ErrorControl
Inherits ContentControl
The ErrorControl represents the icon displayed within an editor when the validation error occurs. The example below shows how to customize the ErrorControl style to provide custom presentation of validation errors.
Example
Please implement the IDataErrorInfo interface on the data object. Then, pass the text and type of error in the IDataErrorInfo.Error property ( it is possible to easily parse this string). Implement a custom style for the ErrorControl. This element presents the error icon. Modify the ErrorControl style in such a way as to take into account a custom error type and text (use the converter).
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>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Editors;
using System.ComponentModel;
namespace WpfApplication147 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
public class TestClass : IDataErrorInfo, INotifyPropertyChanged {
string testString;
public string TestString {
get { return testString; }
set {
testString = value;
RaisePropertyChanged("TestString");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string property) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
#endregion
#region IDataErrorInfo Members
string IDataErrorInfo.Error {
get { return GetError(); }
}
string GetError() {
if (string.IsNullOrEmpty(TestString))
return "ErrorType=Critical;ErrorContent=empty";
if (TestString.Length < 3)
return "ErrorType=Critical;ErrorContent=error";
if (TestString.Length < 5)
return "ErrorType=Information;ErrorContent=warning";
return string.Empty;
}
string IDataErrorInfo.this[string columnName] {
get {
if (columnName == "TestString")
return GetError();
return string.Empty;
}
}
#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
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Windows
Namespace WpfApplication147
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
Inherits Application
End Class
End Namespace
<Application x:Class="WpfApplication147.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
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
See Also