Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi all this is my first question :)

This exemple tested on winform application and wpf application and the problem with binding on WPF
•winform all works fine with ICustomTypeDescriptor and grid draw only columns added to Dictionary Properties (Name Age) and Male excluded
•WPF all properties of the class person drawed on grid (Name Age Male)

any idea about this situation or interfaces equivalent of ICustomTypeDescriptor in WPF ?

XAML
C#
<Grid> 
<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="90,30,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="325" /> 
</Grid> 


Call WinForm and WPF
C#
List<Person> persons = new List<Person>(); 
persons.Add(new Person("Aymane", 30)); 
persons.Add(new Person("Raouia", 30)); 
grid.ItemsSource = persons; //wpf 
grid.DataSource = persons; //winform 


Class
C#
public class Person : ICustomTypeDescriptor 
{ 
    Dictionary<string, object> Properties = new Dictionary<string, object>(); 
 
    public Person() 
    { 
        Properties.Add("Name", null); 
        Properties.Add("Age", null); 
    } 
 
    public Person(string name, object value) 
        : base() 
    { 
        Male = true; 
        Name = name; 
        Age = value; 
    } 
 
    public bool Male { get; set; } 
 
    public object Age { get { return Properties["Age"]; } set { Properties["Age"] = value; } } 
 
    public object Name { get { return Properties["Name"]; } set { Properties["Name"] = value; } } 
 
    #region ICustomTypeDescriptor Members 
 
    AttributeCollection ICustomTypeDescriptor.GetAttributes() 
    { 
        return TypeDescriptor.GetAttributes(this, true); 
    } 
 
    string ICustomTypeDescriptor.GetClassName() 
    { 
        return TypeDescriptor.GetClassName(this, true); 
    } 
 
    string ICustomTypeDescriptor.GetComponentName() 
    { 
        return TypeDescriptor.GetComponentName(this, true); 
    } 
 
    TypeConverter ICustomTypeDescriptor.GetConverter() 
    { 
        return TypeDescriptor.GetConverter(this, true); 
    } 
 
    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() 
    { 
        return TypeDescriptor.GetDefaultEvent(this, true); 
    } 
 
    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() 
    { 
        return TypeDescriptor.GetDefaultProperty(this, true); 
    } 
 
    object ICustomTypeDescriptor.GetEditor(Type editorBaseType) 
    { 
        return TypeDescriptor.GetEditor(this, editorBaseType, true); 
    } 
 
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) 
    { 
        return TypeDescriptor.GetEvents(attributes, true); 
    } 
 
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents() 
    { 
        return ((ICustomTypeDescriptor)this).GetEvents(null); 
    } 
 
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) 
    { 
        List<PropertyDescriptor> props = new List<PropertyDescriptor>(); 
 
        props.Add(new PersonPropertyDescriptor("Name", attributes)); 
        props.Add(new PersonPropertyDescriptor("Age", attributes)); 
 
        return new PropertyDescriptorCollection(props.ToArray()); 
    } 
 
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() 
    { 
        return ((ICustomTypeDescriptor)this).GetProperties(null); 
    } 
 
    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) 
    { 
        return this; 
    } 
 
    #endregion 
 
    class PersonPropertyDescriptor : PropertyDescriptor 
    { 
        public PersonPropertyDescriptor(string name, Attribute[] attrs) 
            : base(name, attrs) 
        { 
        } 
 
        public override bool CanResetValue(object component) 
        { 
            return true; 
        } 
 
        public override Type ComponentType 
        { 
            get { return typeof(Person); } 
        } 
 
        public override object GetValue(object component) 
        { 
            return ((Person)component).Properties[Name]; 
        } 
 
        public override bool IsReadOnly 
        { 
            get { return false; } 
        } 
 
        public override Type PropertyType 
        { 
            get { return typeof(object); } 
        } 
 
        public override void ResetValue(object component) 
        { 
            ((Person)component).Properties[Name] = null; 
        } 
 
        public override void SetValue(object component, object value) 
        { 
            ((Person)component).Properties[Name] = value; 
        } 
 
        public override bool ShouldSerializeValue(object component) 
        { 
            return false; 
        } 
    } 
} 
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900