简体   繁体   English

C# winforms:将 ListBox SelectedItem 绑定到另一个 ListBox

[英]C# winforms: Bind ListBox SelectedItem to another ListBox

I have this model:我有这个 model:

class Car
{
  string CrewNickname { get; set; }
  Líst<string> RacersNames { get; set; }
}

class StartingField
{
  List<Car> Cars { get; set; }
}

What I need is to display cars in one listbox and when I click on any car, to display names of its racers in the second listbox.我需要的是在一个列表框中显示汽车,当我单击任何一辆车时,在第二个列表框中显示其赛车手的姓名。 I also need to be able to add/remove cars and add/remove/edit racers.我还需要能够添加/删除汽车和添加/删除/编辑赛车手。

I haven't found any suitable tutorial or question, but I think this must be possible to create.我还没有找到任何合适的教程或问题,但我认为这一定是可以创建的。

Any help is appreciated.任何帮助表示赞赏。

You should use the OnSelectedIndex method for the first listbox, that raises the SelectedValueChanged event.您应该对引发 SelectedValueChanged 事件的第一个列表框使用 OnSelectedIndex 方法。 Then populate the second listbox based on the selected index of the first listbox.然后根据第一个列表框的选定索引填充第二个列表框。

You can do something like this:你可以这样做:

  • Use two BindingSource objects to link each Car in the list to its list of RacersNames使用两个 BindingSource 对象将列表中的每个 Car 链接到其RacersNames列表
  • Add a Property used to show each Car name in the first ListBox (the CarID Property is there just to show the use of ValueMember )在第一个 ListBox 中添加一个用于显示每个汽车名称的属性( CarID属性只是为了显示ValueMember的使用)
  • You need public Properties, of course.当然,您需要公共属性。

BindingSource bsCars = null;
BindingSource bsRacers = null;
StartingField startingField = null;

public class Car
{
    public Car(string carName, int carID) { CarName = carName; CarID = carID; }
    public string CarName { get; }
    public int CarID { get; }
    public List<string> RacersNames { get; set; } = new List<string>();
}

public class StartingField
{
    public List<Car> Cars { get; } = new List<Car>();
}

The initialize the source of data and set the DataSource of the BindingSource objects and the ListBoxes.初始化数据源并设置BindingSource对象和ListBoxes的DataSource。
Since the second BindingSource is linked to the RacersNames property of the first BindingSource, when you select a car name in the first ListBox, the second BindingSource will update its content and, as a consequence, the ListBox bound to it will show the list of RacersNames related to the new selection.由于第二个 BindingSource 链接到第一个 BindingSource 的RacersNames属性,当您 select 在第一个 ListBox 中的汽车名称时,第二个 BindingSource 将更新其内容,因此,绑定到它的 ListBox 将显示RacersNames列表与新的选择有关。

Elements can be added or removed from both lists.可以从两个列表中添加或删除元素。

You can add this code to the Load event (or OnLoad() override) of a Form.您可以将此代码添加到表单的Load事件(或OnLoad()覆盖)。

startingField = new StartingField();
var c1 = new Car("Car1", 1) { RacersNames = { "Racer1", "Racer2", "Racer3" } };
var c2 = new Car("Car2", 2) { RacersNames = { "Racer4", "Racer5", "Racer6" } };
var c3 = new Car("Car3", 3) { RacersNames = { "Racer7", "Racer8", "Racer9" } };

startingField.Cars.AddRange(new[] { c1, c2, c3});

bsCars = new BindingSource(startingField.Cars, "");
bsRacers = new BindingSource(bsCars, "RacersNames");

listBox1.DisplayMember = "CarName";
listBox1.ValueMember = "CarID";
listBox1.DataSource = bsCars;
listBox2.DataSource = bsRacers;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM