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
Errorproperty 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
TextBoxes 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 theTextBoxto be red when an invalid value is setted and the tooltip of theTextBoxto show the error it got.This is my xaml for TextBox:
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: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: