MVP Winforms和文本框组合框值

时间:2016-04-15 14:03:22

标签: c# winforms combobox mvp

我有一个带有列表作为数据源的组合框。此列表包含具有其属性(名称,地址,...)的对象(客户)。 当我选择组合框的项目时,我想将信息(地址,邮政编码......)传递给我表单上的一些文本框。 在我的测试1tier应用程序中,这是正确的。 但是我正在开发的主要应用程序是基于MVP(我自己触摸它)。我面临的问题是铸造。由于我的观点不知道我的模型,我不应该被允许使用(客户)。 string address = ((Customers)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();
    }
}

我几天来一直在关注这个问题,但未能取得成功。我希望有人能给我正确的方向。

真实应用程序的代码:

查看:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    txtName.Text = comboBox1.SelectedValue.ToString();
}

private void CustomerView_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = customerPresenter.getCustomers();
    comboBox1.DisplayMember = "CustomerName";
    comboBox1.ValueMember = "CustomerId";
}

主讲人:

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

        return customer.ToList();
    }
}

2 个答案:

答案 0 :(得分:5)

这只是实现它的一种方式。您的MVP模式可能看起来不同。 在此实现中,View了解Presenter。有关MVP的更多信息,您可以查看here

您可以将Presenter用作客户的包装器:

if (event.button === 2){
    // run your function
}

Presenter必须实现INotifyPropertyChanged(并在Property setters中调用OnPropertyChanged)。

public interface IPresenter
{
    void Init();
    void SetSelectedCustomer(int customerId);
    IEnumerable GetCustomers();
    string FirstName { get; set; }
    string LastName { get; set; }
    string Address { get; set; }
}

这就是View的样子:

public class Presenter : IPresenter, INotifyPropertyChanged
{
    private readonly Repository _repository;
    private string _firstName;
    private string _lastName;
    private string _address;
    private Customer _currentCustomer;

    public Presenter(Repository repository)
    {
        _repository = repository;
    }

    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value) return;
            _firstName = value;
            OnPropertyChanged();
        }
    }

    public string LastName
    {
        get { return _lastName; }
        set
        {
            if (_lastName == value) return;
            _lastName = value;
            OnPropertyChanged();
        }
    }

    public string Address
    {
        get { return _address; }
        set
        {
            if (_address == value) return;
            _address = value;
            OnPropertyChanged();
        }
    }

    public IEnumerable GetCustomers()
    {
        return _repository.GetAllCustomers();
    }

    public void Init()
    {
        var result = _repository.GetAllCustomers();
        SetSelectedCustomer(result[0].Id);
    }

    public void SetSelectedCustomer(int customerId)
    {
        var customer = _repository.GetCustomerById(customerId);
        FirstName = customer.FirstName;
        LastName = customer.LastName;
        Address = customer.Address;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

您可以从组合框中的SelectedIndexChanged事件中的Presenter设置所选的CustomerId:

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

Presenter中的SetSelectedCustomer方法(或SelectedCustomerChanged事件的EventHandler)选择具有给定CustomerId的Customer并设置FirstName,LastName和Address:

_presenter.SetSelectedCustomer((int)comboBox1.SelectedValue);

您应该在Form_Load中对TextBox进行绑定:

public void SetSelectedCustomer(int customerId)
{
    var customer = _repository.GetCustomerById(customerId);
    FirstName = customer.FirstName;
    LastName = customer.LastName;
    Address = customer.Address;
}

答案 1 :(得分:0)

如果您绝对反对允许您的视图访问您的域对象 - 那些代表数据表行(根据它们的重量,在某些情况下可能没问题) - 您可能希望考虑使用DTO。请查看this以获取有关注意事项的详细说明以及执行此操作的一种方法。请注意底部提到Automapper。这是一个很棒的工具。

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