的WinForms数据绑定 - 绑定到对象列表 [英] WinForms data binding - Bind to objects in a list

查看:138
本文介绍了的WinForms数据绑定 - 绑定到对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的WinForms数据绑定一些帮助/指导,我似乎无法获得谷歌帮助我这一个。

I need some help/guidance on WinForms data binding and I can't seem to get Google to help me with this one.

下面是我的方案。考虑下面的类,类似我所需要的:

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; }
}



我的这种情况将是Car类的一个对象,用List 4胎的对象。请注意,我将永远有一个已知数在这里列表中的对象。

My instances of this will be an object of class Car with a List with four Tire objects. Note that I will always have a known number of objects in the list here.

现在我想将数据绑定这包含五个文本框表单。一个TextBox同车的名字和一个文本框与每个轮胎的压力。

Now I want to data bind this to a Form containing five textboxes. One textbox with the name of the car and one textbox with each of the tires pressures.

如何使这项工作任何想法?在VS设计师似乎并没有让我通过分配列出像轮胎[0]。压力指标进行此设置。

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.

我目前的解决方案是绑定到BindableCar这将是这样的:

My current solution is to bind to a "BindableCar" which would be like:

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; }
    }
}



但是当我的名单包含20本变得很丑陋代替4-对象,并且对于每个欲结合针对6属性的那些对象。这使得一个巨大的BindableObject!

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. That makes a huge "BindableObject"!

推荐答案

虽然WinForms的设计师可能不会让你这样做,你试图建立代码绑定?我想是没有问题的绑定文本框someCar.Tires [1]。压力。

While the WinForms designer may not let you do this, have you tried setting up the binding in code? I imagine there is no problem binding a textbox to someCar.Tires[1].Pressure.

这篇关于的WinForms数据绑定 - 绑定到对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆