You are developing a Silverlight 4 application.
The application contains a Product class that has a public string property named Name.
You create a TextBox control by using the following XAML fragment.
<TextBox Text=”{Binding Name, ValidatesOnDataErrors=True}” />
You need to ensure that validation errors are reported to the user interface. You also need to ensure that a validation error will occur when the TextBox control is empty.
Which code segment should you use?
A.
public class Product
{
[Required()]
public string Name { get; set; }
}
B.
public class Product : IDataErrorInfo
{
public string Name { get; set; }
public string Error { get { return null; } }
public string this[string columnName]
{
get
{
if (columnName == “Name” && string.IsNullOrEmpty(Name))
{
throw new ValidationException(“Name should not be empty! “);
}
return string.Empty;
}}
}
C.
public class Product : IDataErrorInfo
{
public string Name { get; set; }
public string Error { get { return null; } }
public string this[string columnName]
{
get
{
if (columnName == “Name” && string.IsNullOrEmpty(Name))
{
return “Name should not be empty!”;
}
return string.Empty;
}}}
D.
public class Product
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (string.IsNullOrEmpty(value))
throw new ValidationException(“Name should not be empty! “);
_name = value;
}}}