我有一个带文本框的控件。在用户输入几行文本并按钮提交之后,对于每行文本,我需要显示一个带有网格的子窗口,用户必须选择一些值。
假设用户输入5行文本,其中包含客户名称(一行客户名称)。
对于他们每个人点击提交,他必须从ChildWindow中选择销售人员。
当然现在我的循环效果是同时打开5个ChildWindows。
如果用户从Childwindow网格中选择元素后,如何才能获得下一个ChildWindow?
答案 0 :(得分:1)
也许你的控件可以使用看起来像这样的类。
public class SalesPersonSelector
{
private Queue<string> _clientNamesToProcess;
private Dictionary<string, SalesPerson> _selectedSalesPersons;
private Action<IDictionary<string, SalesPerson>> _onComplete;
private string _currentClientName;
public void ProcessNames(IEnumerable<string> clientNames, Action<IDictionary<string, SalesPerson>> onComplete)
{
this._clientNamesToProcess = new Queue<string>(clientNames);
this._selectedSalesPersons = new Dictionary<string, SalesPerson>();
this._onComplete = onComplete;
this.SelectSalespersonForNextClient();
}
private void SelectSalespersonForNextClient()
{
if (this._clientNamesToProcess.Any())
{
this._currentClientName = this._clientNamesToProcess.Dequeue();
ChildWindow childWindow = this.CreateChildWindow(this._currentClientName);
childWindow.Closed += new EventHandler(childWindow_Closed);
childWindow.Show();
}
else
{
this._onComplete(this._selectedSalesPersons);
}
}
private ChildWindow CreateChildWindow(string nextClientName)
{
// TODO: Create child window and give it access to the client name somehow.
throw new NotImplementedException();
}
private void childWindow_Closed(object sender, EventArgs e)
{
var salesPerson = this.GetSelectedSalesPersonFrom(sender as ChildWindow);
this._selectedSalesPersons.Add(this._currentClientName, salesPerson);
this.SelectSalespersonForNextClient();
}
private SalesPerson GetSelectedSalesPersonFrom(ChildWindow childWindow)
{
// TODO: Get the selected salesperson somehow.
throw new NotImplementedException();
}
}
假设您的控件已经将TextBox中的名称拆分为名为“names”的列表,那么您可以这样做:
var salesPersonSelector = new SalesPersonSelector();
salesPersonSelector.ProcessNames(names, selections =>
{
foreach (var selection in selections)
{
var clientName = selection.Key;
var salesPerson = selection.Value;
// TODO: Do something with this information.
}
});
我没有测试过这个,但是Visual Studio并没有给我任何红色的波浪线。