I´m working with windowsapps, and I need to do something different here. I created an object, with some custom properties using ICustomTypeDescriptor with some samples i found on the internet.
Here is my code:
class ObjectWithProperties : ICustomTypeDescriptor
{
**public int MyProperty { get; set; }**
class MyDescriptor : PropertyDescriptor
{
public MyDescriptor(string name, Attribute[] attributes) :
base(name, attributes)
{
}
public override bool CanResetValue(object component)
{
return true;
}
public override Type ComponentType
{
get { return typeof(ObjectWithProperties); }
}
public override object GetValue(object component)
{
return (component as ObjectWithProperties)[Name];
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type PropertyType
{
get { return typeof(object); }
}
public override void ResetValue(object component)
{
(component as ObjectWithProperties).properties.Remove(Name);
}
public override void SetValue(object component, object value)
{
(component as ObjectWithProperties)[Name] = value;
}
public override bool ShouldSerializeValue(object component)
{
return (component as ObjectWithProperties).properties.ContainsKey(Name);
}
}
Dictionary<string, object> properties = new Dictionary<string, object>();
public object this[string name]
{
get
{
if (properties.ContainsKey(name))
{
return properties[name];
}
开发者_开发问答 return null;
}
set
{
properties[name] = value;
}
}
Dictionary<string, List<Attribute>> attributes = new Dictionary<string, List<Attribute>>();
public void SetAttribute(string property, Attribute attribute)
{
if (attributes.ContainsKey(property))
attributes[property].Add(attribute);
else
attributes[property] = new List<Attribute> { attribute };
}
#region ICustomTypeDescriptor Membres
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public string GetClassName()
{
return null;
}
public string GetComponentName()
{
return null;
}
public TypeConverter GetConverter()
{
return null;
}
public EventDescriptor GetDefaultEvent()
{
return null;
}
public PropertyDescriptor GetDefaultProperty()
{
return null;
}
public object GetEditor(Type editorBaseType)
{
return null;
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return EventDescriptorCollection.Empty;
}
public EventDescriptorCollection GetEvents()
{
return EventDescriptorCollection.Empty;
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return new PropertyDescriptorCollection(properties.Keys.Select(key => this.attributes.ContainsKey(key)
? new MyDescriptor(key, this.attributes[key].ToArray()) : new MyDescriptor(key, null)).ToArray());
}
public PropertyDescriptorCollection GetProperties()
{
return new PropertyDescriptorCollection(properties.Keys.Select(key => new MyDescriptor(key, null)).ToArray());
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
//public GetCustomAttribute(string name)
//{
// var bar = TypeDescriptor.GetProperties(this, new Atributos.AtributosdasPropriedades[]{})[0];
// foreach (Attribute attrib in bar.Attributes)
// {
// }
//}
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
var obj1 = new ObjectWithProperties();
obj1["test"] = 100;
var obj2 = new ObjectWithProperties();
obj2["test"] = 200;
var obj3 = new ObjectWithProperties();
obj3["test"] = 150;
**var obj4 = new ObjectWithProperties();
obj4.MyProperty = 100;**
List<ObjectWithProperties> objects = new List<ObjectWithProperties>(new ObjectWithProperties[] { obj1, obj2, obj3, obj4 });
dataGridView1.DataSource = objects;
}
As you can see, is totaly normal. but I need to show the property "MyProperty" on a gridview, with the other ones... Is that possible? Can you please give some sample showing how?
Thank you very much
Please have a look at this https://web.archive.org/web/20210323170551/http://www.4guysfromrolla.com/articles/012308-1.aspx the example if for Webcontrol Gridview , but should guide you in the right direction.
Found a way....
http://jopinblog.wordpress.com/category/design-patterns/
Thank you.
精彩评论