Properties are not displayed in the propertygrid if the class implements ICustomTypeDescriptor. Why?
--
/// <summary>
/// Interaction logic for Myobjects.xaml
/// </summary>
public
partial
class
Myobjects : Page
{
public
Myobjects()
{
InitializeComponent();
radPropertyGrid1.AutoGeneratingPropertyDefinition +=
new
EventHandler<Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs>(radPropertyGrid1_AutoGeneratingPropertyDefinition);
radPropertyGrid1.Item =
new
Variable();
}
void
radPropertyGrid1_AutoGeneratingPropertyDefinition(
object
sender, Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs e)
{
AttributeCollection attributes = TypeDescriptor.GetProperties(
typeof
(Variable))[e.PropertyDefinition.DisplayName].Attributes;
BrowsableAttribute browsable = attributes[
typeof
(BrowsableAttribute)]
as
BrowsableAttribute;
if
(browsable !=
null
)
{
e.Cancel = !browsable.Browsable;
}
//CategoryAttribute category = attributes[typeof(CategoryAttribute)] as CategoryAttribute;
//if (category.Category == "Common")
//{
// e.Cancel = false;
//}
}
}
public
class
Variable : ICustomTypeDescriptor, INotifyPropertyChanged
{
[Browsable(
false
)]
[Category(
"Common"
)]
public
int
Id {
get
;
set
; }
[Category(
"Common"
)]
public
string
Name {
get
;
set
; }
[XmlAttribute(
"mandatoryCondition"
)]
[Category(
"Required Field"
)]
public
string
MandatoryCondition {
get
;
set
; }
[XmlAttribute(
"mandatoryConditionMessage"
)]
[Category(
"Required Field"
)]
public
string
MandatoryConditionMessage {
get
;
set
; }
/// <summary>
/// Datatype of the Variable
/// </summary>
private
DataType dataType = DataType.String;
[XmlAttribute(
"dataType"
)]
[Category(
"Common"
)]
public
DataType DataType
{
get
{
return
this
.dataType;
}
set
{
this
.dataType = value;
NotifyPropertyChanged(
"DataType"
);
}
}
#region ICustomTypeDescriptor Members
public
AttributeCollection GetAttributes()
{
return
TypeDescriptor.GetAttributes(
this
,
true
);
}
public
string
GetClassName()
{
return
TypeDescriptor.GetClassName(
this
,
true
);
}
public
string
GetComponentName()
{
return
null
;
}
public
TypeConverter GetConverter()
{
return
TypeDescriptor.GetConverter(
this
,
true
);
}
public
EventDescriptor GetDefaultEvent()
{
return
TypeDescriptor.GetDefaultEvent(
this
,
true
);
}
public
PropertyDescriptor GetDefaultProperty()
{
return
TypeDescriptor.GetDefaultProperty(
this
,
true
);
}
public
object
GetEditor(Type editorBaseType)
{
return
null
;
}
public
EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return
TypeDescriptor.GetEvents(
this
, attributes,
true
);
}
public
EventDescriptorCollection GetEvents()
{
return
TypeDescriptor.GetEvents(
this
,
true
);
}
public
object
GetPropertyOwner(PropertyDescriptor pd)
{
return
this
;
}
public
PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return
this
.GetProperties();
}
// Method implemented to expose mandatory field properties conditionally, depending on DataType property
public
PropertyDescriptorCollection GetProperties()
{
var props =
new
PropertyDescriptorCollection(
null
);
foreach
(PropertyDescriptor prop
in
TypeDescriptor.GetProperties(
this
,
true
))
{
if
(prop.Category ==
"Common"
&&
this
.DataType != DataType.String)
continue
;
if
(prop.Category ==
"Range"
&& (
this
.DataType != DataType.Integer &&
this
.DataType != DataType.Float))
continue
;
if
(prop.Category ==
"Required Field"
)
continue
;
props.Add(prop);
}
return
props;
}
#endregion
#region INotifyPropertyChanged Members
public
event
PropertyChangedEventHandler PropertyChanged;
private
void
NotifyPropertyChanged(String info)
{
if
(PropertyChanged !=
null
)
{
PropertyChanged(
this
,
new
PropertyChangedEventArgs(info));
}
}
#endregion
}
public
enum
DataType
{
String,
Integer,
Float,
Boolean,
Date
}