我有一个组合框,其中有一个列表作为数据源。此列表包含具有其属性(名称,地址等)的对象(客户)。当我选择组合框的一个项目时,我想将信息(地址,邮政编码...)传递给表单上的某些文本框。在我的测试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();
}
}
这只是实现它的一种方法。您的MVP模式可能看起来有所不同。在此实现中,视图了解演示者。有关MVP的更多信息,请点击此处
您可以将Presenter用作客户的包装器:
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 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));
}
}
这是视图的样子:
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事件中的Presenter上设置选定的CustomerId:
_presenter.SetSelectedCustomer((int)comboBox1.SelectedValue);
Presenter中的SetSelectedCustomer方法(或SelectedCustomerChanged事件的EventHandler)选择具有给定CustomerId的Customer并设置FirstName,LastName和Address:
public void SetSelectedCustomer(int customerId)
{
var customer = _repository.GetCustomerById(customerId);
FirstName = customer.FirstName;
LastName = customer.LastName;
Address = customer.Address;
}
您应该在Form_Load中为TextBoxes进行绑定:
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)));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句