[英]How to change the item source of "comboboxB"using selection from "comboboxA" of windows form C#
Two Combo-Boxes are added to a Windows form dynamically through code.通过代码将两个组合框动态添加到 Windows 表单中。 Now the based on the selection from the Combo-Box-A i want to change the item source of Combo-Box-B.
现在基于从 Combo-Box-A 中的选择,我想更改 Combo-Box-B 的项目源。
Here are some snippets这是一些片段
Adding combo boxes dynamically动态添加组合框
string[] comboboxAitemsource = { "1", "2", "3", "4"};
string[] itemsource_1 = { "11", "12", "13", "14"};
string[] itemsource_2 = { "21", "22", "23", "24"};
string[] itemsource_3 = { "31", "32", "33", "34"};
string[] itemsource_4 = { "41", "42", "43", "44"};
int left_margin = 10, top_margin = margin, margin_inc = 40, sec_left_margin = 300;
System.Windows.Controls.Label ComboboxAlabel = new System.Windows.Controls.ComboboxAlabel();
ComboboxAlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
ComboboxAlabel.VerticalAlignment = VerticalAlignment.Top;
ComboboxAlabel.Name = "typeComboboxAlabel";
ComboboxAlabel.Content = "COMBOBOX A";
ComboboxAlabel.Height = 40;
ComboboxAlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(ComboboxAlabel);
System.Windows.Controls.ComboBox comboboxA = new System.Windows.Controls.ComboBox();
comboboxA.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
comboboxA.VerticalAlignment = VerticalAlignment.Top;
comboboxA.Name = "typeCombobox";
comboboxA.Height = 20;
comboboxA.Width = 250;
comboboxA.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxA.ItemsSource = comboboxAitemsource;
comboboxA.SelectedIndex = 0;
comboboxA.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(typeCombobox_SelectionChangedEventHandler);
top_margin = top_margin + margin_inc;
grid.Children.Add(comboboxA);
System.Windows.Controls.Label comboboxBlabel = new System.Windows.Controls.Label();
comboboxBlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxBlabel.VerticalAlignment = VerticalAlignment.Top;
comboboxBlabel.Name = "typeLabelUpdate";
comboboxBlabel.Content = "COMBOBOX B";
comboboxBlabel.Height = 40;
comboboxBlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(comboboxBlabel);
System.Windows.Controls.ComboBox comboboxB = new System.Windows.Controls.ComboBox();
comboboxB.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxB.VerticalAlignment = VerticalAlignment.Top;
comboboxB.Name = "typeComboBoxUpdate";
comboboxB.Height = 20;
comboboxB.Width = 250;
comboboxB.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxB.ItemsSource = itemsource_1;
comboboxB.SelectedIndex = 0;
top_margin = top_margin + margin_inc;
grid.Children.Add(comboboxB);
Event handler for combo box A组合框 A 的事件处理程序
private void typeCombobox_SelectionChangedEventHandler(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
}
By default item source for combo box B is itemsource_1, but when the selection changes on combo box A the item source of combo box b should change based on the selection.默认情况下,组合框 B 的项目源是 itemsource_1,但是当组合框 A 的选择发生变化时,组合框 b 的项目源应该根据选择而改变。
for example if selection of combo box A changes to 4 the item source of combo box B should change to itemsource_4.例如,如果组合框 A 的选择更改为 4,则组合框 B 的项目源应更改为 itemsource_4。
I have added a event handler but I'm unable to access the combo box b in the event handler.我添加了事件处理程序,但无法访问事件处理程序中的组合框 b。 Any help would be really appreciated..
任何帮助将非常感激..
i think:我认为:
private void comboxa_SelectedIndexChanged(object sender, EventArgs e)
{
comboxb.Items.Clear();
comboxb.Items.AddRange(itemsource_1);
}
private void d()
{
string[] comboboxAitemsource = { "1", "2", "3", "4" };
string[] itemsource_1 = { "11", "12", "13", "14" };
string[] itemsource_2 = { "21", "22", "23", "24" };
string[] itemsource_3 = { "31", "32", "33", "34" };
string[] itemsource_4 = { "41", "42", "43", "44" };
int left_margin = 10, top_margin = margin, margin_inc = 40, sec_left_margin = 300;
System.Windows.Controls.Label ComboboxAlabel = new System.Windows.Controls.ComboboxAlabel();
ComboboxAlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
ComboboxAlabel.VerticalAlignment = VerticalAlignment.Top;
ComboboxAlabel.Name = "typeComboboxAlabel";
ComboboxAlabel.Content = "COMBOBOX A";
ComboboxAlabel.Height = 40;
ComboboxAlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(ComboboxAlabel);
System.Windows.Controls.ComboBox comboboxA = new System.Windows.Controls.ComboBox();
comboboxA.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
comboboxA.VerticalAlignment = VerticalAlignment.Top;
comboboxA.Name = "typeCombobox";
comboboxA.Height = 20;
comboboxA.Width = 250;
comboboxA.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxA.ItemsSource = comboboxAitemsource;
comboboxA.SelectedIndex = 0;
comboboxA.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(typeCombobox_SelectionChangedEventHandler);
top_margin = top_margin + margin_inc;
grid.Children.Add(comboboxA);
System.Windows.Controls.Label comboboxBlabel = new System.Windows.Controls.Label();
comboboxBlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxBlabel.VerticalAlignment = VerticalAlignment.Top;
comboboxBlabel.Name = "typeLabelUpdate";
comboboxBlabel.Content = "COMBOBOX B";
comboboxBlabel.Height = 40;
comboboxBlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(comboboxBlabel);
System.Windows.Controls.ComboBox comboboxB = new System.Windows.Controls.ComboBox();
comboboxB.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxB.VerticalAlignment = VerticalAlignment.Top;
comboboxB.Name = "typeComboBoxUpdate";
comboboxB.Height = 20;
comboboxB.Width = 250;
comboboxB.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxB.ItemsSource = itemsource_1;
comboboxB.SelectedIndex = 0;
top_margin = top_margin + margin_inc;
grid.Children.Add(comboboxB);
comboboxA.SelectedIndexChanged += (s, e) => {
comboboxB.Items.Clear();
comboboxB.Items.AddRange(itemsource_1);
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.