About Me

My photo
I'm an undergraduate of Sri Lanka Institute of Information Technology(SLIIT).

Combobox items in wpf forms

Hello everyone!!!

You may use drop down lists in your projects recently. If you want to change the items in a combobox according to the selection of previous combobox what you will do? I’m going to tell how I do that in my wpf form.
In my case when it selects a mathematical operation it should load relevant formats to another combobox.

 ComboBoxItem item0 = new ComboBoxItem();
        ComboBoxItem item1 = new ComboBoxItem();
        ComboBoxItem item2 = new ComboBoxItem();
        ComboBoxItem item3 = new ComboBoxItem();
        ComboBoxItem item4 = new ComboBoxItem();
        ComboBoxItem item5 = new ComboBoxItem();
        ComboBoxItem item6 = new ComboBoxItem();
        ComboBoxItem item7 = new ComboBoxItem();
        ComboBoxItem item8 = new ComboBoxItem();
        ComboBoxItem item9 = new ComboBoxItem();
        ComboBoxItem item10 = new ComboBoxItem();
        ComboBoxItem item11 = new ComboBoxItem();

     if (cmbsimpleOperation.SelectedIndex == 0)
            {
                cmbFormat.IsEnabled = true;//enable format selection

                //load relevent items to index 0
                item0.Content = "A+B";
                item0.Background = Brushes.White;
                item0.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item0);

                item1.Content = "A+B'";
                item1.Background = Brushes.White;
                item1.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item1);

                item2.Content = "A'+B";
                item2.Background = Brushes.White;
                item2.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item2);

                item3.Content = "A'+B'";
                item3.Background = Brushes.White;
                item3.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item3);

                //remove already loaded items
                cmbFormat.Items.Remove(item4);
                cmbFormat.Items.Remove(item5);
                cmbFormat.Items.Remove(item6);
                cmbFormat.Items.Remove(item7);

                cmbFormat.Items.Remove(item8);
                cmbFormat.Items.Remove(item9);
                cmbFormat.Items.Remove(item10);
                cmbFormat.Items.Remove(item11);

          
            }

            if (cmbsimpleOperation.SelectedIndex == 1)
            {
                cmbFormat.IsEnabled = true;//enable format selection

                //load relevent items to index 1
                item4.Content = "A-B";
                item4.Background = Brushes.White;
                item4.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item4);

                item5.Content = "A-B'";
                item5.Background = Brushes.White;
                item5.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item5);

                item6.Content = "A'-B";
                item6.Background = Brushes.White;
                item6.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item6);

                item7.Content = "A'-B'";
                item7.Background = Brushes.White;
                item7.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item7);

                //removealready loaded items
                cmbFormat.Items.Remove(item0);
                cmbFormat.Items.Remove(item1);
                cmbFormat.Items.Remove(item2);
                cmbFormat.Items.Remove(item3);

                cmbFormat.Items.Remove(item8);
                cmbFormat.Items.Remove(item9);
                cmbFormat.Items.Remove(item10);
                cmbFormat.Items.Remove(item11);

               
            }

            if (cmbsimpleOperation.SelectedIndex == 2)
            {
                cmbFormat.IsEnabled = true;//enable format selection

                //load relevent items to index 2
                item8.Content = "A*B";
                item8.Background = Brushes.White;
                item8.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item8);

                item9.Content = "A*B'";
                item9.Background = Brushes.White;
                item9.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item9);

                item10.Content = "A'*B";
                item10.Background = Brushes.White;
                item10.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item10);

                item11.Content = "A'*B'";
                item11.Background = Brushes.White;
                item11.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item11);

                //remove alreadyloaded items
                cmbFormat.Items.Remove(item0);
                cmbFormat.Items.Remove(item1);
                cmbFormat.Items.Remove(item2);
                cmbFormat.Items.Remove(item3);

                cmbFormat.Items.Remove(item4);
                cmbFormat.Items.Remove(item5);
                cmbFormat.Items.Remove(item6);
                cmbFormat.Items.Remove(item7);

               
            }

This is little bit different format comparing to windows form. Try this out.
Hope this post will help to you.
Don’t forget to leave a comment. Your suggestions are highly welcome.
Cheers!!!



0 comments:

Post a Comment