C# MVP Winforms和textbox组合框值

C# MVP Winforms和textbox组合框值,c#,winforms,combobox,mvp,C#,Winforms,Combobox,Mvp,我有一个列表作为数据源的组合框。此列表包含对象(客户)及其属性(名称、地址等)。 当我选择组合框中的一项时,我想将信息(地址、zipcode…)传递给表单上的一些文本框。 在我的测试1层应用程序中,这是正确的。 但是我正在开发的主要应用程序是基于MVP的(我自己也有这个想法)。我面临的问题是铸造。由于我的视图不知道我的模型,因此不应允许我使用(客户)字符串地址=((客户)comboBox1.SelectedItem).CustomerAddress 1层测试代码: private void co

我有一个列表作为数据源的组合框。此列表包含对象(客户)及其属性(名称、地址等)。 当我选择组合框中的一项时,我想将信息(地址、zipcode…)传递给表单上的一些文本框。 在我的测试1层应用程序中,这是正确的。 但是我正在开发的主要应用程序是基于MVP的(我自己也有这个想法)。我面临的问题是铸造。由于我的视图不知道我的模型,因此不应允许我使用(客户)<代码>字符串地址=((客户)comboBox1.SelectedItem).CustomerAddress

1层测试代码:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //getCustomers((int)comboBox1.SelectedValue);
    //txtAddress.Text =Convert.ToString( comboBox1.SelectedValue);
    Customers p = (Customers)comboBox1.SelectedItem;
    string s = comboBox1.SelectedItem.ToString();
    string address = ((Customers)comboBox1.SelectedItem).CustomerAddress;
    txtAddress1.Text = address;
}

private void Form3_Load(object sender, EventArgs e)
{
    using (var emp = new EmployerEFEntities())
    {
        var query = from customers in emp.Customers
                    select customers;

        comboBox1.DisplayMember = "CustomerName";
        comboBox1.ValueMember = "CustomerID";
        comboBox1.DataSource = query.ToList();
    }
}
我一直在寻找这几天了,但还没有成功。我希望有人能给我正确的方向

实际应用程序的代码:

视图:

演示者:

public List<tbl_customer> getCustomers()
{
    using (var customers = new DBCrownfishEntities())
    {
        var customer = from c in customers.tbl_customer
                       select c;

        return customer.ToList();
    }
}
public List getCustomers()
{
使用(var customers=new DBCrownfishEntities())
{
var customer=来自c in customers.tbl\U customer
选择c;
return customer.ToList();
}
}

如果您绝对反对允许您的视图访问域对象——那些表示数据表行的对象(在某些情况下,根据它们的重量,这些行可能没问题)——您可能需要研究使用DTO。请查看有关注意事项的详细说明和一种实现方法。注意下面提到的。这是一个很棒的工具


编辑:我刚刚意识到您对Winforms解决方案感兴趣,而不是ASP.NET解决方案。尽管上面的链接涉及ASP.NET,但Winforms应用程序的想法是相同的。

这只是实现它的一种方法。您的MVP模式可能看起来有所不同。 在这个实现中,视图知道演示者。有关MVP的更多信息,请查看

您可以将演示者用作客户的包装:

public interface IPresenter
{
    void Init();
    void SetSelectedCustomer(int customerId);
    IEnumerable GetCustomers();
    string FirstName { get; set; }
    string LastName { get; set; }
    string Address { get; set; }
}
演示者必须实现INotifyPropertyChanged(并在属性设置器中调用OnPropertyChanged)

这是视图的外观:

public partial class Form1 : Form
{
    private IPresenter _presenter;
    private bool _initialized;

    public Form1(IPresenter presenter)
    {
        InitializeComponent();           
        _presenter = presenter;
        _presenter.Init();
        SetComboBoxData(_presenter.GetCustomers());
        _initialized = true;
    }

    public void SetComboBoxData(IEnumerable data)
    {
        comboBox1.DataSource = data;
        comboBox1.ValueMember = "Id";
        comboBox1.DisplayMember = "FirstName";
    }

    private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        if (!_initialized) return;
        _presenter.SetSelectedCustomer((int)comboBox1.SelectedValue);
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        textBox1.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.FirstName)));
        textBox2.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.LastName)));
        textBox3.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.Address)));
    }
}
您可以在组合框的SelectedIndexChanged事件中的演示者处设置选定的CustomerId:

_presenter.SetSelectedCustomer((int)comboBox1.SelectedValue);
演示者中的SetSelectedCustomer方法(或SelectedCustomerChanged事件的EventHandler)选择具有给定CustomerId的客户,并设置FirstName、LastName和Address:

public void SetSelectedCustomer(int customerId)
{
    var customer = _repository.GetCustomerById(customerId);
    FirstName = customer.FirstName;
    LastName = customer.LastName;
    Address = customer.Address;
}
您应该在表单_Load中对文本框进行绑定:

textBox1.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.FirstName)));
textBox2.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.LastName)));
textBox3.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.Address)));
public void SetSelectedCustomer(int customerId)
{
    var customer = _repository.GetCustomerById(customerId);
    FirstName = customer.FirstName;
    LastName = customer.LastName;
    Address = customer.Address;
}
textBox1.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.FirstName)));
textBox2.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.LastName)));
textBox3.DataBindings.Add(new Binding("Text", _presenter, nameof(_presenter.Address)));