2012-03-04 143 views
0

大家好,这是我的第一个问题:)动态绑定WPF

本例在WinForm应用程序和WPF应用程序,并与

  • WinForm的所有WPF的约束力问题测试的正常工作与ICustomTypeDescriptor和电网平局只列添加到字典属性(名称岁)和男排除
  • WPF类人的所有属性drawed网格(姓名年龄男)

关于这种情况或wpf中ICustomTypeDescriptor的等价接口的任何想法?

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

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 

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; 
     } 
    } 
} 
+2

什么性别歧视的数据结构的... – 2012-03-04 02:37:37

回答

0

这里的正确实施ICustomTypeDescriptor & ITypedList

namespace CustomTypeDescriptor 

{

class Row : ICustomTypeDescriptor { } 

class RowsCollection : List<Row>, ITypedList { } 

class Table : IListSource, IEnumerable<Row>, IEnumerator<Row> 
{ 
    RowsCollection Rows { get; set; } 
} 

}

0

为了获得控制权,列生成处理AutoGeneratingColumn事件,你可以通过塞汀e.Cancel = true;

在你的情况下抑制列的代:

private void DataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{ 
    var dataGrid = sender as DataGrid; 
    if (dataGrid != null) 
    { 
     ICustomTypeDescriptor typeDescriptor = 
      dataGrid.Items[0] as ICustomTypeDescriptor; 
     if (typeDescriptor != null) 
     { 
      var props = typeDescriptor.GetProperties(); 

      if (!props.Contains((PropertyDescriptor)e.PropertyDescriptor)) 
      { 
       e.Cancel = true; 
      } 
     } 
    } 
} 

随着数据网格定义:

<DataGrid 
     AutoGenerateColumns="True" 
     Height="311" 
     HorizontalAlignment="Left" 
     Name="dataGrid1" 
     VerticalAlignment="Top"   
     Width="509" 
     AutoGeneratingColumn="DataGridAutoGeneratingColumn"> 

给出所需的结果。

+0

我需要生成列自动我为什么make autogeneratecolumns = true – Xgamerz 2012-03-08 00:38:58

+0

@Xgamerz:我的错误,我纠正了我的答案 – 2012-03-08 05:56:45