我有一个组合框,上面有一个作为数据源的列表。此列表包含对象(客户)及其属性(名称、地址、.)。当我选择一个项目的组合框,我想传递的信息(地址,邮政编码.)我的表格上的一些文本框。在我的测试1层应用程序中,这是正确的。但是我所使用的主要应用是基于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();
}
}发布于 2016-05-27 14:04:05
这只是实现它的一种方法。你的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 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)));
}
}您可以从您的组合框中在您的CustomerId事件的演示者处设置选定的SelectedIndexChanged:
_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;
}您应该在TextBoxes中用Form_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)));发布于 2016-04-15 15:24:16
如果您完全反对允许您的视图访问您的域对象--那些表示数据表行的对象(根据它们的大小,在某些情况下可能还可以)--那么您可能需要考虑使用DTO。请查看这,以获得对注意事项的良好描述和一种方法。请注意底部提到的自动机。这是个很棒的工具。
编辑:我刚意识到你对Winforms解决方案感兴趣,而不是ASP.NET。虽然上面的链接涉及到ASP.NET,但对于Winforms应用程序来说,想法是一样的。
https://stackoverflow.com/questions/36649323
复制相似问题