这是我为动态生成控件而编写的代码:
private void GenCtrl(string type, int typeid,string sql)
{
string id = Convert.ToString(typeid);
if (type == "TextBox")
{// created for TextBox
}
else if (type == "ComboBox")
{
ComboBoxEdit cb = new ComboBoxEdit();
cb.Name = "cb" + id;
// this is to set unique id if more than 1 combobox created.
cb.Height = 20;
cb.Width = 100;
cb.Margin = new Thickness(2, 2, 0, 0);
cb.HorizontalAlignment = HorizontalAlignment.Left;
cb.Text = "Show All";
stctrl.Children.Add(cb);
ComboBoxEdit cbx = (ComboBoxEdit)cb.FindName(cb.Name);
cb.DisplayMember = "Name";
if(filsql !=null)
ServRef.GetComboBoxlistAsync(sql); //this would retrieve the list and attach to the combobox.
//ctlst.Add(cb.Name, type);
}
}
// WCF服务参考
ServRef.GetComboBoxlistCompleted += new EventHandler<GetComboBoxlistCompletedEventArgs>(ServRef_GetComboBoxlistCompleted);
void ServRef_GetComboBoxlistCompleted(object sender, GetComboBoxlistCompletedEventArgs e)
{
cb.ItemsSource = e.Result;
cb.SelectedIndex = 0;
}
我在绑定两个组合框时面临的问题是创建ex:cb1,cb2(动态创建)仅控制cb2 combox,这是最新在ServRef_GetComboBoxlistCompleted方法中激活的。我无法绑定第一个控件(组合框)的值。
如果我的问题不明确,请告诉我。
答案 0 :(得分:0)
我能够找到使用FindName
ComboBoxEdit cbx = (ComboBoxEdit)stackctrl.FindName(cb.Name);