Reputation: 83
I have two combo Boxes in WPF application.
I have added checkboxes dynamically to the first Combo Boxes. If I select one of the check boxes from the first ComboBox, the checkbox event should be handled to add some checkboxes to the Second Combo Box.
I have tried, but I didn't work.
the code.
private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
AddAnalytes();
}
public void AddCartridges()
{
for (int i = 0; i < cartridges.Length; i++)
{
CheckBox cbox = new CheckBox();
// cbox.Name = "cbox" + cartridges[i].ToString();
cbox.Content = cartridges[i];
this.CartridgeCombo.Items.Add(cbox);
//CheckBox[] cartridgeBoxes= new
}
}
public void AddAnalytes()
{
if (this.CartridgeCombo.SelectedItem.ToString() == "CHEM8")
{
Analytes = new string[] { "NA", "K", "CL", "TCO2", "BUN", "CREA", "EGFR", "GLU", "CA", "ANG", "HCT", "HGB" };
for (int i = 0; i < Analytes.Length; i++)
{
CheckBox cb = new CheckBox();
cb.Name = "cb" + Analytes[i];
cb.Content = Analytes[i];
this.AnalyteCombo.Items.Add(cb);
}
}
Upvotes: 1
Views: 487
Reputation: 1937
You have not used MVVM at all - so this will eventually get bit tricky for you - as everything is code behind. Below works for me:
private void PopulateCheckboxes1()
{
for(int idx = 0; idx < 5; idx++)
{
var chkBox = new CheckBox();
chkBox.Content = string.Format($"TextBox: {idx}");
chkBox.Tag = idx;
chkBox.Checked += ChkBox_Checked;
cmbBox1.Items.Add(chkBox);
}
}
private void ChkBox_Checked(object sender, RoutedEventArgs e)
{
var itemsToAdd = (int)(sender as Control).Tag;
cmbBox2.Items.Clear();
for (int idx = 0; idx < itemsToAdd; idx++)
{
var chkBox = new CheckBox();
chkBox.Content = string.Format($"TextBox: {idx}");
cmbBox2.Items.Add(chkBox);
}
}
Put your custom logic in ChkBox_Checked handler. My logic is to just add several check boxes to combo box 2 based on the Tag property.
Upvotes: 1