2012-12-11 4 views
3

제네릭 컬렉션 목록 연결 to propGrid 바인딩을 시도했지만 출력이 예상 한 것과 일치하지 않습니다. 나는 을 원합니다처럼 표시 될 것입니다. ListBoxpropGrid입니다. 어떻게해야합니까? 고맙습니다.제네릭 컬렉션 바인딩 목록 <>> 속성 표 배열

class Contact 
{ 
    public string Name { get; set; } 
    public string Address { get; set; } 
} 

PropertyGrid propGrid = new PropertyGrid(); 
List<Contact> listContact = new List<Contact>(); 

private void Form1_Load(object sender, EventArgs e) 
{ 
    listContact.Clear(); 
    Contact newContact = null; 

    newContact = new Contact(); 
    newContact.Name = "diana"; 
    newContact.Address = "en"; 
    listContact.Add(newContact); 

    newContact = null; 
    newContact = new Contact(); 
    newContact.Name = "maxim"; 
    newContact.Address = "cand"; 
    listContact.Add(newContact); 

    propGrid.SelectedObject = listContact; 
    this.Controls.Add(propGrid); 
    propGrid.Dock = DockStyle.Fill; 

} 
+0

이 주소는 이미 이름이 없습니다? http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx – ccook

답변

5

ExpandableObjectConverter을 사용하려면 수업을 연장해야합니다. 이것은 parseable을 발생시킵니다.

아래 코드를 참조하십시오. 예를 들어 떨어졌다. 가장 좋아하는 것을 선택하십시오. 코드를 사용 소스 : MSDN

[TypeConverter(typeof(Contact))] 
[DescriptionAttribute("Expand to see the spelling options for the application.")] 
class Contact : ExpandableObjectConverter 
{ 
    [DefaultValueAttribute("Contact Name")] 
    public string Name { get; set; } 
    public string Address { get; set; } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     if (destinationType == typeof(Contact)) 
     { 
      return true; 
     } 
     return base.CanConvertTo(context, destinationType); 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) 
    { 
     if (destinationType == typeof(System.String) && value is Contact) 
     { 
      Contact contact = value as Contact; 

      return string.Format("Name: {0} - Address: {1}", contact.Name, contact.Address); 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 
} 

[TypeConverter(typeof(Contact2))] 
[DescriptionAttribute("Expand to see the spelling options for the application.")] 
class Contact2 : ExpandableObjectConverter 
{ 
    private Contact contato1; 
    public Contact Contato1 
    { 
     get 
     { 
      return contato1; 
     } 
     set 
     { 
      contato1 = value; 
     } 
    } 

    private Contact contato3; 
    public Contact Contato3 
    { 
     get 
     { 
      return contato3; 
     } 
     set 
     { 
      contato3 = value; 
     } 
    } 

    public Contact2() 
    { 
     this.contato1 = new Contact() 
     { 
      Address = "Add1", 
      Name = "Name1" 
     }; 
     this.contato3 = new Contact() 
     { 
      Address = "Add3", 
      Name = "Name3" 
     }; 
    } 
} 

public partial class Form2 : Form 
{ 
    PropertiesList<Contact> listContact = new PropertiesList<Contact>(); 
    //List<Contact> listContact = new List<Contact>(); 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void Form2_Load(object sender, EventArgs e) 
    { 
     listContact.Clear(); 
     Contact newContact = null; 

     newContact = new Contact(); 
     newContact.Name = "diana"; 
     newContact.Address = "en"; 
     listContact.Add(newContact); 

     newContact = null; 
     newContact = new Contact(); 
     newContact.Name = "maxim"; 
     newContact.Address = "cand"; 
     listContact.Add(newContact); 

     propGrid.AllowDrop = true; 

     object[] itens = new object[2]; 
     itens[0] = listContact; 
     itens[1] = newContact; 
     propGrid.SelectedObject = listContact; 
     this.Controls.Add(propGrid); 
     propGrid.Dock = DockStyle.Fill; 
    } 
} 

[TypeConverter(typeof(Contact))] 
class PropertiesList<T> : ExpandableObjectConverter where T : Contact 
{ 
    private List<T> innerList = new List<T>(); 

    public List<string> Names 
    { 
     get 
     { 
      List<string> valuesReturned = null; 
      if (innerList.Count > 0) 
      { 
       valuesReturned = new List<string>(); 
       for (int i = 0; i < innerList.Count; i++) 
       { 
        valuesReturned.Add(innerList[i].Name); 
       } 

      } 
      return valuesReturned; 
     } 
    } 

    public List<T> Item 
    { 
     get 
     { 
      List<T> valuesReturned = null; 
      if (innerList.Count > 0) 
      { 
       valuesReturned = new List<T>(); 
       for (int i = 0; i < innerList.Count; i++) 
       { 
        valuesReturned.Add(innerList[i]); 
       } 

      } 
      return valuesReturned; 
     } 
    } 

    public Color GetColors { get; set; } 

    public Contact2 Contato22 
    { 
     get 
     { 
      return new Contact2(); 
     } 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     if (destinationType == typeof(Contact)) 
     { 
      return true; 
     } 
     return base.CanConvertTo(context, destinationType); 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) 
    { 
     if (destinationType == typeof(System.String) && value is Contact) 
     { 
      Contact contact = value as Contact; 

      return string.Format("Name: {0} - Address: {1}", contact.Name, contact.Address); 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 

    #region Simulate List 
    public void Add(T item) 
    { 
     innerList.Add(item); 
    } 

    public void Clear() 
    { 
     innerList.Clear(); 
    } 
    #endregion 
} 
+0

위대한 코드, 감사합니다. –

관련 문제