Я создал winform с двумя списками: listbox1 и listbox2, и вот как выглядит мой Form1.cs
namespace WinFormsApp
{
public partial class Form1 : Form
{
private List<Category> categories;
public Form1()
{
InitializeComponent();
categories = new List<Category>();
var categoryOne = new Category { Name = "Category 1"} ;
categoryOne.Items.Add( new CategoryItem { Name = "Item 1"} );
var categoryTwo = new Category { Name = "Category 2" };
categoryTwo.Items.Add( new CategoryItem { Name = "Item 2" } );
categories.Add( categoryOne );
categories.Add( categoryTwo );
}
private void Form1_Load(object sender, System.EventArgs e)
{
categoryBindingSource.DataSource = categories;
}
}
public class Category
{
public string Name { get; set; }
public List<CategoryItem> Items { get; private set; }
public Category()
{
Items = new List<CategoryItem>();
}
}
public class CategoryItem
{
public string Name { get; set; }
}
}
и вот код InitializeComponent ()
this.listBox1.DataSource = this.categoryBindingSource;
this.listBox1.DisplayMember = "Name";
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(24, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(242, 238);
this.listBox1.TabIndex = 0;
this.listBox1.ValueMember = "Items";
this.categoryBindingSource.DataSource = typeof(Category);
this.listBox2.DataSource = this.itemsBindingSource;
this.listBox2.FormattingEnabled = true;
this.listBox2.Location = new System.Drawing.Point(286, 24);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(276, 238);
this.listBox2.TabIndex = 1;
this.listBox2.ValueMember = "Name";
this.itemsBindingSource.DataMember = "Items";
this.itemsBindingSource.DataSource = this.categoryBindingSource;
![enter image description here](https://i.stack.imgur.com/Vh0BT.png)