简体   繁体   English

WinForms数据绑定 - 绑定到列表中的对象

[英]WinForms data binding - Bind to objects in a list

I need some help/guidance on WinForms data binding and I can't seem to get Google to help me with this one. 我需要一些关于WinForms数据绑定的帮助/指导,我似乎无法让Google帮助我解决这个问题。

Here is my scenario. 这是我的情景。 Consider the following classes which is similar to what I need: 考虑以下类似于我需要的类:

public class Car
{
    public string Name { get; set; }
    public List<Tire> Tires { get; set; }
}

public class Tire
{
    public double Pressure { get; set; }
}

My instances of this will be an object of class Car with a List with four Tire objects. 我的实例将是Car类的一个对象,其中包含一个带有四个Tire对象的List。 Note that I will always have a known number of objects in the list here. 请注意,我将在列表中始终具有已知数量的对象。

Now I want to data bind this to a Form containing five textboxes. 现在我想将数据绑定到包含五个文本框的Form。 One textbox with the name of the car and one textbox with each of the tires pressures. 一个带有汽车名称的文本框和一个带有每个轮胎压力的文本框。

Any idea on how to make this work? 有关如何使这项工作的任何想法? The designer in VS does not seem to allow me to set this up by assigning to list indexes like Tires[0].Pressure. VS中的设计者似乎不允许我通过分配像Tires [0] .Pressure这样的列表索引来设置它。

My current solution is to bind to a "BindableCar" which would be like: 我目前的解决方案是绑定到“BindableCar”,就像:

public class BindableCar
{
    private Car _car;

    public BindableCar(Car car)
    {
        _car = car;
    }

    public string Name
    {
        get { return _car.Name; }
        set { _car.Name = value; }
    }

    public double Tire1Pressure
    {
        get { return _car.Tires[0].Pressure; }
        set { _car.Tires[0].Pressure = value; }
    }

    public double Tire2Pressure
    {
        get { return _car.Tires[1].Pressure; }
        set { _car.Tires[1].Pressure = value; }
    }

    public double Tire3Pressure
    {
        get { return _car.Tires[2].Pressure; }
        set { _car.Tires[2].Pressure = value; }
    }

    public double Tire4Pressure
    {
        get { return _car.Tires[3].Pressure; }
        set { _car.Tires[3].Pressure = value; }
    }
}

but this becomes really ugly when my lists contains 20 instead of 4 objects, and for each of those objects I want to bind against 6 properties. 但是当我的列表包含20个而不是4个对象时,这变得非常难看,并且对于每个这些对象,我想要绑定6个属性。 That makes a huge "BindableObject"! 这是一个巨大的“BindableObject”!

You should note that you can bind controls to any object type that implements the interfaces IList , ICollection or IEnumerable or inherits from classes that implement these interfaces. 您应该注意,您可以将控件绑定到任何实现接口IListICollectionIEnumerable对象类型,或者从实现这些接口的类继承。 Generic collections also qualify for this kind of binding. 通用集合也有资格进行这种绑定。

These are internally converted to an IBindingList instance. 这些内部转换为IBindingList实例。

Check out the following links for more information: 有关更多信息,请查看以下链接:

  1. Roadmap for Windowsforms databinding - Takes a very comprehensive look at the implementation and possibilities and provides a variety of links to other KB articles. Windowsform数据绑定路线图 - 全面了解实现和可能性,并提供各种知识链接到其他知识库文章。
  2. Winforms Object binding - Rockford Lhotka's article on the subject. Winforms对象绑定 - Rockford Lhotka关于这个主题的文章。 Provides a Windows forms designer-oriented way of implementing databinding. 提供面向Windows窗体设计器的实现数据绑定的方法。
  3. Databinding with Windows forms 2.0 - This book by Brian Noyes explores various aspects of Databinding, both in complex and simple scenarios. 使用Windows窗体2.0进行数据绑定 - Brian Noyes的这本书探讨了数据绑定的各个方面,包括复杂和简单的场景。

While the WinForms designer may not let you do this, have you tried setting up the binding in code? 虽然WinForms设计师可能不允许您这样做,但您是否尝试在代码中设置绑定? I imagine there is no problem binding a textbox to someCar.Tires[1].Pressure. 我想将文本框绑定到someCar.Tires [1]没有问题。压力。

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

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