将 class 对象绑定到组合框(当没有 class 的引用时)
Binding class object to a Combobox (when there is no reference of that class)
在UI层,我想从class的绑定列表中绑定一个组合框。此 class 是作为单独项目可用的业务实体,未在 UL 层中引用。
//UI Layer
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "CustomerID";
comboBox1.DataSource = new CustomerFacade().getCustomers();
}
}
//Facade Class
public class CustomerFacade
{
public BindingList<Customer> getCustomers()
{
BindingList<Customer> objects = new BindingList<Customer>();
for (int i = 0; i < 10; i++)
{
objects.Add(new Customer() { Name = "Customer " + i.ToString(), CustomerID = i });
}
return objects;
}
}
//Business Entity Class
public class Customer
{
public Int32 CustomerID { get; set; }
public string Name { get; set; }
}
这里我在同一项目中声明了客户 class,所以它工作正常。但是,如果我将此客户 class 保留在业务实体中(作为一个单独的项目),那么如果不添加业务实体的引用,它将无法工作。
如何在不添加业务实体引用的情况下加载值或绑定此组合框?
是否有其他方法,如将其转换为数组或数组列表,这将绑定我的组合框?
在您的 Business Entity
中创建例如 DataTable
并将其用作数据源。
查看 this thread 了解详情。
或者您可以创建字符串数组(使用 toString
方法)。取决于您要向用户显示的内容。
您需要的是您的客户的接口 class。将此接口放在一个新项目中(通常此项目称为 Contracts)。每个项目都应参考合同项目。通过这种方式,您可以将实现接口的对象传递给另一个只需要知道接口的项目。
我修改了您的代码以显示它。 (我没有测试)
//UI Layer
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "CustomerID";
comboBox1.DataSource = new CustomerFacade().getCustomers();
}
}
//Facade Class
public class CustomerFacade
{
public BindingList<ICustomer> getCustomers()
{
BindingList<ICustomer> objects = new BindingList<ICustomer>();
for (int i = 0; i < 10; i++)
{
objects.Add(new Customer() { Name = "Customer " + i.ToString(), CustomerID = i });
}
return objects;
}
}
//Business Entity
public class Customer : ICustomer
{
public Int32 CustomerID { get; set; }
public string Name { get; set; }
}
//Contracts
public interface ICustomer
{
Int32 CustomerID { get; set; }
string Name { get; set; }
}
也许您可以通过搜索 "dependency injection" 找到更多相关信息。我想这就是您要找的。
在UI层,我想从class的绑定列表中绑定一个组合框。此 class 是作为单独项目可用的业务实体,未在 UL 层中引用。
//UI Layer
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "CustomerID";
comboBox1.DataSource = new CustomerFacade().getCustomers();
}
}
//Facade Class
public class CustomerFacade
{
public BindingList<Customer> getCustomers()
{
BindingList<Customer> objects = new BindingList<Customer>();
for (int i = 0; i < 10; i++)
{
objects.Add(new Customer() { Name = "Customer " + i.ToString(), CustomerID = i });
}
return objects;
}
}
//Business Entity Class
public class Customer
{
public Int32 CustomerID { get; set; }
public string Name { get; set; }
}
这里我在同一项目中声明了客户 class,所以它工作正常。但是,如果我将此客户 class 保留在业务实体中(作为一个单独的项目),那么如果不添加业务实体的引用,它将无法工作。
如何在不添加业务实体引用的情况下加载值或绑定此组合框? 是否有其他方法,如将其转换为数组或数组列表,这将绑定我的组合框?
在您的 Business Entity
中创建例如 DataTable
并将其用作数据源。
查看 this thread 了解详情。
或者您可以创建字符串数组(使用 toString
方法)。取决于您要向用户显示的内容。
您需要的是您的客户的接口 class。将此接口放在一个新项目中(通常此项目称为 Contracts)。每个项目都应参考合同项目。通过这种方式,您可以将实现接口的对象传递给另一个只需要知道接口的项目。 我修改了您的代码以显示它。 (我没有测试)
//UI Layer
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "CustomerID";
comboBox1.DataSource = new CustomerFacade().getCustomers();
}
}
//Facade Class
public class CustomerFacade
{
public BindingList<ICustomer> getCustomers()
{
BindingList<ICustomer> objects = new BindingList<ICustomer>();
for (int i = 0; i < 10; i++)
{
objects.Add(new Customer() { Name = "Customer " + i.ToString(), CustomerID = i });
}
return objects;
}
}
//Business Entity
public class Customer : ICustomer
{
public Int32 CustomerID { get; set; }
public string Name { get; set; }
}
//Contracts
public interface ICustomer
{
Int32 CustomerID { get; set; }
string Name { get; set; }
}
也许您可以通过搜索 "dependency injection" 找到更多相关信息。我想这就是您要找的。