2012-03-04 2 views
0

Привет всем это мой первый вопрос :)динамическое связывание МОФ

Это Exemple испытания по применению Winform и применение МОФ и проблема с обязательным WPF

  • WinForm все отлично работает с ICustomTypeDescriptor и сетки розыгрыша только столбцы добавлены в словарь свойства (имя) Возраст и мужчина, исключенный
  • WPF все свойства класса человека нарисовали на сетке (Имя Возраст Мужской)

любая идея об этой ситуации или эквивалент интерфейса ICustomTypeDescriptor в wpf?

<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

Каких сексистские структур данных ... –

ответ

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 из:

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

дает желаемый результат.

+0

мне нужно генерировать столбцы автоматическими, что почему я make autogeneratecolumns = true – Xgamerz

+0

@Xgamerz: Моя ошибка, я исправил свой ответ –

Смежные вопросы