OVH Cloud OVH Cloud

Utilisation de ICustomTypeDescriptor

2 réponses
Avatar
Kassim Machioudi
Bonjour,

J'aimerais utiliser un property grid pour afficher uniquement quelques
propriétés d'un control. Pour cela, j'ai créer une classe dérivée de
UserControl et qui implémente ICustomDescriptor. Cf. code ci-dessous. Mon
approche semble marcher, sauf que toutes les propriétés n'apparaissent dans
le PropertyGrid à l'exécution. Quelqu'un aurait une idée de ce qui ne marche
pas ?

Merci,

---

class WBBaseControl : UserControl, ICustomTypeDescriptor
{
Control control ;

private PropertyDescriptorCollection propList ;
private static string [] allowProperties = new string [] {"Name", "Text",
"Location", "Width", "Height"} ;

public WBBaseControl (Control control)
{
this.control = control ;
this.control.Dock = DockStyle.Fill ;
this.Size = this.control.Size ;

this.Controls.Add (this.control) ;
}

protected override void OnTextChanged (EventArgs e)
{
base.OnTextChanged (e) ;
control.Text = this.Text ;
}

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return TypeDescriptor.GetProperties(this, true);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

public PropertyDescriptorCollection GetProperties()
{
if (propList == null)
{
PropertyDescriptorCollection defaultProp =
TypeDescriptor.GetProperties(this, true);

propList = new PropertyDescriptorCollection(null) ;

foreach (PropertyDescriptor pd in defaultProp)
{
if (IsAllowedProp(pd.Name))
{//Console.WriteLine (pd.Name) ;
propList.Add (pd) ;
}
}
}
Console.WriteLine (propList.Count) ;
return propList ;
}

private static bool IsAllowedProp (string propName)
{
foreach (string name in allowProperties)
{
if (propName == name)
{
//Console.WriteLine (propName) ;
return true ;
}
}

return false ;
}

}

2 réponses

Avatar
Paul Bacelar
http://msdn.microsoft.com/msdnmag/issues/05/04/NETMatters/
http://msdn.microsoft.com/msdnmag/issues/05/05/NETMatters/default.aspx
--
Paul Bacelar
MVP VC++


"Kassim Machioudi" wrote in message
news:
Bonjour,

J'aimerais utiliser un property grid pour afficher uniquement quelques
propriétés d'un control. Pour cela, j'ai créer une classe dérivée de
UserControl et qui implémente ICustomDescriptor. Cf. code ci-dessous. Mon
approche semble marcher, sauf que toutes les propriétés n'apparaissent
dans le PropertyGrid à l'exécution. Quelqu'un aurait une idée de ce qui ne
marche pas ?

Merci,

---

class WBBaseControl : UserControl, ICustomTypeDescriptor
{
Control control ;

private PropertyDescriptorCollection propList ;
private static string [] allowProperties = new string [] {"Name",
"Text", "Location", "Width", "Height"} ;

public WBBaseControl (Control control)
{
this.control = control ;
this.control.Dock = DockStyle.Fill ;
this.Size = this.control.Size ;

this.Controls.Add (this.control) ;
}

protected override void OnTextChanged (EventArgs e)
{
base.OnTextChanged (e) ;
control.Text = this.Text ;
}

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return TypeDescriptor.GetProperties(this, true);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

public PropertyDescriptorCollection GetProperties()
{
if (propList == null)
{
PropertyDescriptorCollection defaultProp > TypeDescriptor.GetProperties(this, true);

propList = new PropertyDescriptorCollection(null) ;

foreach (PropertyDescriptor pd in defaultProp)
{
if (IsAllowedProp(pd.Name))
{//Console.WriteLine (pd.Name) ;
propList.Add (pd) ;
}
}
}
Console.WriteLine (propList.Count) ;
return propList ;
}

private static bool IsAllowedProp (string propName)
{
foreach (string name in allowProperties)
{
if (propName == name)
{
//Console.WriteLine (propName) ;
return true ;
}
}

return false ;
}

}



Avatar
Kassim Machioudi
Ok merci, je vais consulter l'article.

"Paul Bacelar" wrote in message
news:
http://msdn.microsoft.com/msdnmag/issues/05/04/NETMatters/
http://msdn.microsoft.com/msdnmag/issues/05/05/NETMatters/default.aspx
--
Paul Bacelar
MVP VC++


"Kassim Machioudi" wrote in message
news:
Bonjour,

J'aimerais utiliser un property grid pour afficher uniquement quelques
propriétés d'un control. Pour cela, j'ai créer une classe dérivée de
UserControl et qui implémente ICustomDescriptor. Cf. code ci-dessous. Mon
approche semble marcher, sauf que toutes les propriétés n'apparaissent
dans le PropertyGrid à l'exécution. Quelqu'un aurait une idée de ce qui
ne marche pas ?

Merci,

---

class WBBaseControl : UserControl, ICustomTypeDescriptor
{
Control control ;

private PropertyDescriptorCollection propList ;
private static string [] allowProperties = new string [] {"Name",
"Text", "Location", "Width", "Height"} ;

public WBBaseControl (Control control)
{
this.control = control ;
this.control.Dock = DockStyle.Fill ;
this.Size = this.control.Size ;

this.Controls.Add (this.control) ;
}

protected override void OnTextChanged (EventArgs e)
{
base.OnTextChanged (e) ;
control.Text = this.Text ;
}

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}

public PropertyDescriptorCollection GetProperties(Attribute[]
attributes)
{
return GetProperties();
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return TypeDescriptor.GetProperties(this, true);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

public PropertyDescriptorCollection GetProperties()
{
if (propList == null)
{
PropertyDescriptorCollection defaultProp >> TypeDescriptor.GetProperties(this, true);

propList = new PropertyDescriptorCollection(null) ;

foreach (PropertyDescriptor pd in defaultProp)
{
if (IsAllowedProp(pd.Name))
{//Console.WriteLine (pd.Name) ;
propList.Add (pd) ;
}
}
}
Console.WriteLine (propList.Count) ;
return propList ;
}

private static bool IsAllowedProp (string propName)
{
foreach (string name in allowProperties)
{
if (propName == name)
{
//Console.WriteLine (propName) ;
return true ;
}
}

return false ;
}

}