I'm currently looking into validation for my WPF app and seen the mention of IDataErrorInfo
. However there are few guides to how to use it and worse there are none who explain how it works.
On MSND.com site this is given: MSDN
public class Person : IDataErrorInfo
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public string Error
{
get
{
return null;
}
}
public string this[string name]
{
get
{
string result = null;
if (name == "Age")
{
if (this.age < 0 || this.age > 150)
{
result = "Age must not be less than 0 or greater than 150.";
}
}
return result;
}
}
}
I see what's going on here but I have no idea what it really does to my data.
When are those 2 properties used? Let's say someone sets Age
as 400: the setter on the property is called. Will the error thingy stop it from being set? If not and it just warns that the number is incorrect, what's to stop someone from saving the info as is? There is no IsValid()
method to check, is there?
Would love to know what happens behind the curtains.
The Error
property is not usually used, but you have to define it in order to implement the interface.
As decyclone said, Validation won't stop the property from being set with the wrong value, but you could set the property to a default value.
Let me show you how I'm using it. I have a couple of TextBox
es that I have to validate the values they have. Instead of showing a MessageBox with the error when the set is called, I want to take a 'webly' approach: I want the border and the background of the TextBox
to be red when an invalid value is setted and the tooltip of the TextBox
to show the error it got.
This is my xaml for TextBox:
<converters:ValidationConverter x:Key="validationConverter"/>
<Style x:Key="TestStepTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource validationConverter}, Path=(Validation.Errors)}"/>
<Setter Property="Background" Value="#33FF342D"/>
<Setter Property="BorderBrush" Value="#AAFF342D"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<TextBox Name="txtRunAfter" Text="{Binding RunAfter, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource TestStepTextBox}"/>
<TextBox Name="txtStopAfter" Text="{Binding StopAfter, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource TestStepTextBox}"/>
A very important note about the converter. I was getting an exception when I entered an invalid value and then I set a good value. Somehow, maybe related to having UpdateSourceTrigger=PropertyChanged
, there was a time when the HasError property was true but there were no error set (see link). So here is the code for the Converter:
public class ValidationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ReadOnlyObservableCollection<ValidationError> errors = value as ReadOnlyObservableCollection<ValidationError>;
if (errors == null) return value;
if (errors.Count > 0)
{
return errors[0].ErrorContent;
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("This method should never be called");
}
}
To prevent the invaled value from being saved to my model layer I'm using the same method to check if I should commit the data to the model. If the value is invalid I just set the property and don't call a set of the property in the model. Check the code:
private int _runAfter = 0;
public int RunAfter
{
get
{
return _runAfter;
}
set
{
if (_runAfter != value)
{
_runAfter = value;
OnPropertyChanged("RunAfter");
if (validateRunAfter() == null)
setRunAfter(); //sets the property value to the model layer
}
}
}
string IDataErrorInfo.this[string columnName]
{
get
{
string message = null;
if (columnName == "RunAfter")
message = validateRunAfter();
//...
return message;
}
}
private string validateRunAfter()
{
if (value >= _order)
return "Run After value must be less than its Step Order (#) value.";
return null;
}
What I know of IDataErrorInfo
is it is used for UI
purposes only. What it does is provide an easy way to bind the error message(s) to the WPF UI
. Since WPF UI
"identifies" the objects that implement IDataErrorInfo
just like INotifyPropertyChanged
, you don't have to write extra code to show the error messages in the UI
.
One more thing, it will not stop from being set the wrong value. It will just tell the WPF UI
(when WPF UI
calls the indexer supplying property name) that the value in a specific property is not valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With