Category / Section
How to set the different validation for the same datatype of different properties in PropertyGrid
1 min read
We can set different validation of a same datatype of properties in PropertyGrid by comparing Name property of PropertyInfo class.
The following example will provide a better idea about it. In this example integer datatype is set using CustomEditor. The validation for 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: