How to set the different validation for the same datatype of different properties in PropertyGrid
We can set different validations for the same datatype for properties in the PropertyGrid by comparing the Name property of the PropertyInfo class.
The following example will provide a better idea about it. In this example, the integer datatype is set using CustomEditor. The validation for the integer is made for the field “Age” and “Year”.
XAML
<syncfusion:PropertyGrid x:Name="pgrid" />
C#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
this.pgrid.SelectedObject = new Customer();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
CustomEditor upDownEditor = new CustomEditor() { HasPropertyType = true, PropertyType = typeof(int) };
upDownEditor.Editor = new UpDownEditor();
pgrid.CustomEditorCollection.Add(upDownEditor);
pgrid.RefreshPropertygrid();
}
}
public class Customer
{
private string _name;
private int _age;
private int _year;
private string _SSN;
private string _address;
private string _email;
public string Name
{
get{return _name;
set{_name = value;}
}
public int Year
{
get{return _year;}
set{_year = value;}
}
public string SSN
{
get{return _SSN;}
set{_SSN = value;}
}
public string Address
{
get{return _address;}
set{_address = value;}
}
public int Age
{
get{return _age;}
set{_age = value;}
}
public string Email
{
get{return _email;}
set{_email = value;}
}
}
public class UpDownEditor : ITypeEditor
{
public void Attach(PropertyViewItem property, PropertyItem info)
{ }
UpDown upDown;
public object Create(PropertyInfo propertyInfo)
{
upDown = new UpDown(){ };
if (propertyInfo.Name == "Age")
{
upDown.MinValue = 2;
upDown.MaxValue = 8;
upDown.NumberDecimalDigits = 0;
}
if (propertyInfo.Name == "Year")
{
upDown.MinValue = 1920;
upDown.MaxValue = 2000;
upDown.NumberDecimalDigits = 0;
}
return upDown;
}
public void Detach(PropertyViewItem property)
{
throw new NotImplementedException();
}
}
The output for the above code is shown below:
