首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MVP Winforms和textbox组合框值

MVP Winforms和textbox组合框值
EN

Stack Overflow用户
提问于 2016-04-15 14:03:21
回答 2查看 1.5K关注 0票数 3

我有一个组合框,上面有一个作为数据源的列表。此列表包含对象(客户)及其属性(名称、地址、.)。当我选择一个项目的组合框,我想传递的信息(地址,邮政编码.)我的表格上的一些文本框。在我的测试1层应用程序中,这是正确的。但是我所使用的主要应用是基于MVP (有我自己的触觉)。我面临的问题是铸造。由于我的观点不知道我的模型,我不应该被允许使用(客户)。string address = ((Customers)comboBox1.SelectedItem).CustomerAddress;

1层测试代码:

代码语言:javascript
运行
复制
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();
    }
}

我已经看了好几天了,但还没有成功。我希望有人能给我正确的方向。

实际应用程序的代码:

查看:

代码语言:javascript
运行
复制
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";
}

主持人:

代码语言:javascript
运行
复制
public List<tbl_customer> getCustomers()
{
    using (var customers = new DBCrownfishEntities())
    {
        var customer = from c in customers.tbl_customer
                       select c;

        return customer.ToList();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-27 14:04:05

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

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

代码语言:javascript
运行
复制
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 )。

代码语言:javascript
运行
复制
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));
    }
}

视图就是这样的:

代码语言:javascript
运行
复制
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)));
    }
}

您可以从您的组合框中在您的CustomerId事件的演示者处设置选定的SelectedIndexChanged:

代码语言:javascript
运行
复制
_presenter.SetSelectedCustomer((int)comboBox1.SelectedValue);

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

代码语言:javascript
运行
复制
public void SetSelectedCustomer(int customerId)
{
    var customer = _repository.GetCustomerById(customerId);
    FirstName = customer.FirstName;
    LastName = customer.LastName;
    Address = customer.Address;
}

您应该在TextBoxes中用Form_Load进行绑定:

代码语言:javascript
运行
复制
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)));
票数 5
EN

Stack Overflow用户

发布于 2016-04-15 15:24:16

如果您完全反对允许您的视图访问您的域对象--那些表示数据表行的对象(根据它们的大小,在某些情况下可能还可以)--那么您可能需要考虑使用DTO。请查看,以获得对注意事项的良好描述和一种方法。请注意底部提到的自动机。这是个很棒的工具。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36649323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档